<?php

namespace App\Http\Controllers\Api\V1\Documents;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Document\ApproveRequest;
use App\Http\Requests\Document\BulkCreateFqcRequest;
use App\Http\Requests\Document\IndexRequest;
use App\Http\Requests\Document\RejectRequest;
use App\Http\Requests\Document\ResolveRequest;
use App\Http\Requests\Document\StoreRequest;
use App\Http\Requests\Document\UpdateRequest;
use App\Http\Requests\Document\UpsertRequest;
use App\Services\DocumentService;
use Illuminate\Http\JsonResponse;

class DocumentController extends Controller
{
    public function __construct(private DocumentService $service) {}

    /**
     * 문서 목록 조회
     * GET /v1/documents
     */
    public function index(IndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->list($request->validated());
        }, __('message.fetched'));
    }

    /**
     * 문서 상세 조회
     * GET /v1/documents/{id}
     */
    public function show(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.fetched'));
    }

    /**
     * 문서 생성
     * POST /v1/documents
     */
    public function store(StoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->create($request->validated());
        }, __('message.created'));
    }

    /**
     * 문서 수정
     * PATCH /v1/documents/{id}
     */
    public function update(int $id, UpdateRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update($id, $request->validated());
        }, __('message.updated'));
    }

    /**
     * 문서 삭제
     * DELETE /v1/documents/{id}
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroy($id);
        }, __('message.deleted'));
    }

    // =========================================================================
    // FQC 일괄생성 (제품검사)
    // =========================================================================

    /**
     * 수주 개소별 제품검사 문서 일괄생성
     * POST /v1/documents/bulk-create-fqc
     */
    public function bulkCreateFqc(BulkCreateFqcRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->bulkCreateFqc($request->validated());
        }, __('message.created'));
    }

    /**
     * 수주 FQC 진행현황 조회
     * GET /v1/documents/fqc-status?order_id=1&template_id=65
     */
    public function fqcStatus(): JsonResponse
    {
        return ApiResponse::handle(function () {
            $orderId = (int) request('order_id');
            $templateId = (int) request('template_id');

            if (! $orderId || ! $templateId) {
                throw new \Symfony\Component\HttpKernel\Exception\BadRequestHttpException(
                    __('validation.required', ['attribute' => 'order_id, template_id'])
                );
            }

            return $this->service->fqcStatus($orderId, $templateId);
        }, __('message.fetched'));
    }

    // =========================================================================
    // Resolve/Upsert (React 연동용)
    // =========================================================================

    /**
     * 문서 Resolve
     * GET /v1/documents/resolve?category=incoming_inspection&item_id=12596
     */
    public function resolve(ResolveRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->resolve($request->validated());
        }, __('message.fetched'));
    }

    /**
     * 문서 Upsert
     * POST /v1/documents/upsert
     */
    public function upsert(UpsertRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->upsert($request->validated());
        }, __('message.saved'));
    }

    // =========================================================================
    // 결재 워크플로우
    // =========================================================================

    /**
     * 결재 제출 (DRAFT → PENDING)
     * POST /v1/documents/{id}/submit
     */
    public function submit(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->submit($id);
        }, __('message.updated'));
    }

    /**
     * 결재 승인
     * POST /v1/documents/{id}/approve
     */
    public function approve(int $id, ApproveRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->approve($id, $request->validated()['comment'] ?? null);
        }, __('message.updated'));
    }

    /**
     * 결재 반려
     * POST /v1/documents/{id}/reject
     */
    public function reject(int $id, RejectRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->reject($id, $request->validated()['comment']);
        }, __('message.updated'));
    }

    /**
     * 결재 취소/회수
     * POST /v1/documents/{id}/cancel
     */
    public function cancel(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->cancel($id);
        }, __('message.updated'));
    }
}
