<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
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('material_ids', []));
        }, __('message.work_order.material_input_registered'));
    }
}
