<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\WorkOrder\MaterialInputForItemRequest;
use App\Http\Requests\WorkOrder\StoreItemInspectionRequest;
use App\Http\Requests\WorkOrder\WorkOrderAssignRequest;
use App\Http\Requests\WorkOrder\WorkOrderIssueRequest;
use App\Http\Requests\WorkOrder\WorkOrderStatusRequest;
use App\Http\Requests\WorkOrder\WorkOrderStoreRequest;
use App\Http\Requests\WorkOrder\WorkOrderUpdateRequest;
use App\Services\WorkOrderService;
use Illuminate\Http\Request;

class WorkOrderController extends Controller
{
    public function __construct(private WorkOrderService $service) {}

    /**
     * 목록 조회
     */
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->all());
        }, __('message.work_order.fetched'));
    }

    /**
     * 통계 조회
     */
    public function stats()
    {
        return ApiResponse::handle(function () {
            return $this->service->stats();
        }, __('message.work_order.fetched'));
    }

    /**
     * 단건 조회
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 생성
     */
    public function store(WorkOrderStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.work_order.created'));
    }

    /**
     * 수정
     */
    public function update(WorkOrderUpdateRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->update($id, $request->validated());
        }, __('message.work_order.updated'));
    }

    /**
     * 삭제
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->destroy($id);

            return 'success';
        }, __('message.work_order.deleted'));
    }

    /**
     * 상태 변경
     */
    public function updateStatus(WorkOrderStatusRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->updateStatus($id, $request->validated()['status']);
        }, __('message.work_order.status_updated'));
    }

    /**
     * 담당자 배정
     */
    public function assign(WorkOrderAssignRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->assign($id, $request->validated());
        }, __('message.work_order.assigned'));
    }

    /**
     * 벤딩 항목 토글
     */
    public function toggleBendingField(Request $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->toggleBendingField($id, $request->input('field'));
        }, __('message.work_order.bending_toggled'));
    }

    /**
     * 이슈 추가
     */
    public function addIssue(WorkOrderIssueRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->addIssue($id, $request->validated());
        }, __('message.work_order.issue_added'));
    }

    /**
     * 이슈 해결
     */
    public function resolveIssue(int $workOrderId, int $issueId)
    {
        return ApiResponse::handle(function () use ($workOrderId, $issueId) {
            return $this->service->resolveIssue($workOrderId, $issueId);
        }, __('message.work_order.issue_resolved'));
    }

    /**
     * 품목 상태 변경
     */
    public function updateItemStatus(Request $request, int $workOrderId, int $itemId)
    {
        return ApiResponse::handle(function () use ($request, $workOrderId, $itemId) {
            return $this->service->updateItemStatus($workOrderId, $itemId, $request->input('status'));
        }, __('message.work_order.item_status_updated'));
    }

    /**
     * 자재 목록 조회
     */
    public function materials(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getMaterials($id);
        }, __('message.work_order.materials_fetched'));
    }

    /**
     * 자재 투입 등록 (로트별 수량 차감)
     */
    public function registerMaterialInput(Request $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->registerMaterialInput($id, $request->input('inputs', []));
        }, __('message.work_order.material_input_registered'));
    }

    /**
     * 공정 단계 진행 현황 조회
     */
    public function stepProgress(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getStepProgress($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 공정 단계 완료 토글
     */
    public function toggleStepProgress(Request $request, int $id, int $progressId)
    {
        return ApiResponse::handle(function () use ($id, $progressId) {
            return $this->service->toggleStepProgress($id, $progressId);
        }, __('message.work_order.updated'));
    }

    /**
     * 자재 투입 이력 조회
     */
    public function materialInputHistory(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getMaterialInputHistory($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 자재 투입 LOT 번호 조회 (stock_transactions 기반)
     */
    public function materialInputLots(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getMaterialInputLots($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 품목별 중간검사 데이터 저장
     */
    public function storeItemInspection(StoreItemInspectionRequest $request, int $workOrderId, int $itemId)
    {
        return ApiResponse::handle(function () use ($request, $workOrderId, $itemId) {
            return $this->service->storeItemInspection($workOrderId, $itemId, $request->validated());
        }, __('message.work_order.inspection_saved'));
    }

    /**
     * 작업지시 전체 품목 검사 데이터 조회
     */
    public function inspectionData(Request $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->getInspectionData($id, $request->all());
        }, __('message.work_order.fetched'));
    }

    /**
     * 작업지시 검사 성적서용 데이터 조회
     */
    public function inspectionReport(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getInspectionReport($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 작업지시 검사 설정 조회 (공정 자동 판별 + 구성품 목록)
     */
    public function inspectionConfig(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getInspectionConfig($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 작업지시의 검사용 문서 템플릿 조회
     */
    public function inspectionTemplate(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getInspectionTemplate($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 검사 문서 resolve (기존 문서 조회 또는 생성 정보 반환)
     */
    public function resolveInspectionDocument(Request $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->resolveInspectionDocument($id, $request->all());
        }, __('message.fetched'));
    }

    /**
     * 검사 완료 시 검사 문서(Document) 생성/수정
     */
    public function createInspectionDocument(Request $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->createInspectionDocument($id, $request->all());
        }, __('message.work_order.inspection_document_created'));
    }

    /**
     * 작업일지 양식 템플릿 조회
     */
    public function workLogTemplate(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getWorkLogTemplate($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 작업일지 조회 (기존 문서 + 템플릿 + 통계)
     */
    public function workLog(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getWorkLog($id);
        }, __('message.work_order.fetched'));
    }

    /**
     * 작업일지 생성/수정
     */
    public function createWorkLog(Request $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->createWorkLog($id, $request->all());
        }, __('message.work_order.work_log_saved'));
    }

    // ──────────────────────────────────────────────────────────────
    // 개소별 자재 투입
    // ──────────────────────────────────────────────────────────────

    /**
     * 개소별 자재 목록 조회
     */
    public function materialsForItem(int $id, int $itemId)
    {
        return ApiResponse::handle(function () use ($id, $itemId) {
            return $this->service->getMaterialsForItem($id, $itemId);
        }, __('message.work_order.materials_fetched'));
    }

    /**
     * 개소별 자재 투입 등록
     */
    public function registerMaterialInputForItem(MaterialInputForItemRequest $request, int $id, int $itemId)
    {
        return ApiResponse::handle(function () use ($request, $id, $itemId) {
            $validated = $request->validated();

            return $this->service->registerMaterialInputForItem(
                $id,
                $itemId,
                $validated['inputs'],
                (bool) ($validated['replace'] ?? false)
            );
        }, __('message.work_order.material_input_registered'));
    }

    /**
     * 개소별 자재 투입 이력 조회
     */
    public function materialInputsForItem(int $id, int $itemId)
    {
        return ApiResponse::handle(function () use ($id, $itemId) {
            return $this->service->getMaterialInputsForItem($id, $itemId);
        }, __('message.work_order.fetched'));
    }

    public function deleteMaterialInput(int $id, int $inputId)
    {
        return ApiResponse::handle(function () use ($id, $inputId) {
            $this->service->deleteMaterialInput($id, $inputId);
        }, __('message.work_order.deleted'));
    }

    public function updateMaterialInput(Request $request, int $id, int $inputId)
    {
        $data = $request->validate([
            'qty' => ['required', 'numeric', 'gt:0'],
        ]);

        return ApiResponse::handle(function () use ($id, $inputId, $data) {
            return $this->service->updateMaterialInput($id, $inputId, (float) $data['qty']);
        }, __('message.work_order.updated'));
    }
}
