<?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\IndexRequest;
use App\Http\Requests\Document\RejectRequest;
use App\Http\Requests\Document\StoreRequest;
use App\Http\Requests\Document\UpdateRequest;
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'));
    }

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

    /**
     * 결재 제출 (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'));
    }
}
