<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Approval\InboxIndexRequest;
use App\Http\Requests\Approval\IndexRequest;
use App\Http\Requests\Approval\ReferenceIndexRequest;
use App\Http\Requests\Approval\RejectRequest;
use App\Http\Requests\Approval\StoreRequest;
use App\Http\Requests\Approval\SubmitRequest;
use App\Http\Requests\Approval\UpdateRequest;
use App\Services\ApprovalService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ApprovalController extends Controller
{
    public function __construct(private ApprovalService $service) {}

    /**
     * 기안함 - 내가 기안한 문서 목록
     * GET /v1/approvals/drafts
     */
    public function drafts(IndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->drafts($request->validated());
        }, __('message.fetched'));
    }

    /**
     * 기안함 현황 카드
     * GET /v1/approvals/drafts/summary
     */
    public function draftsSummary(): JsonResponse
    {
        return ApiResponse::handle(function () {
            return $this->service->draftsSummary();
        }, __('message.fetched'));
    }

    /**
     * 결재함 - 내가 결재해야 할 문서 목록
     * GET /v1/approvals/inbox
     */
    public function inbox(InboxIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->inbox($request->validated());
        }, __('message.fetched'));
    }

    /**
     * 결재함 현황 카드
     * GET /v1/approvals/inbox/summary
     */
    public function inboxSummary(): JsonResponse
    {
        return ApiResponse::handle(function () {
            return $this->service->inboxSummary();
        }, __('message.fetched'));
    }

    /**
     * 참조함 - 내가 참조된 문서 목록
     * GET /v1/approvals/reference
     */
    public function reference(ReferenceIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->reference($request->validated());
        }, __('message.fetched'));
    }

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

    /**
     * 결재 문서 생성 (임시저장 또는 상신)
     * POST /v1/approvals
     */
    public function store(StoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.approval.created'));
    }

    /**
     * 결재 문서 수정 (임시저장 상태만)
     * PATCH /v1/approvals/{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/approvals/{id}
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroy($id);
        }, __('message.deleted'));
    }

    /**
     * 결재 상신
     * POST /v1/approvals/{id}/submit
     */
    public function submit(int $id, SubmitRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->submit($id, $request->validated());
        }, __('message.approval.submitted'));
    }

    /**
     * 결재 승인
     * POST /v1/approvals/{id}/approve
     */
    public function approve(int $id, Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->approve($id, $request->input('comment'));
        }, __('message.approval.approved'));
    }

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

    /**
     * 결재 회수 (기안자만)
     * POST /v1/approvals/{id}/cancel
     */
    public function cancel(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->cancel($id);
        }, __('message.approval.cancelled'));
    }

    /**
     * 참조 열람 처리
     * POST /v1/approvals/{id}/read
     */
    public function markRead(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->markRead($id);
        }, __('message.approval.marked_read'));
    }

    /**
     * 참조 미열람 처리
     * POST /v1/approvals/{id}/unread
     */
    public function markUnread(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->markUnread($id);
        }, __('message.approval.marked_unread'));
    }
}
