<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Approval\FormIndexRequest;
use App\Http\Requests\Approval\FormStoreRequest;
use App\Http\Requests\Approval\FormUpdateRequest;
use App\Services\ApprovalService;
use Illuminate\Http\JsonResponse;

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

    /**
     * 결재 양식 목록
     * GET /v1/approval-forms
     */
    public function index(FormIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->formIndex($request->validated());
        }, __('message.fetched'));
    }

    /**
     * 활성 결재 양식 목록 (셀렉트박스용)
     * GET /v1/approval-forms/active
     */
    public function active(): JsonResponse
    {
        return ApiResponse::handle(function () {
            return $this->service->formActive();
        }, __('message.fetched'));
    }

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

    /**
     * 결재 양식 생성
     * POST /v1/approval-forms
     */
    public function store(FormStoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->formStore($request->validated());
        }, __('message.approval.form_created'));
    }

    /**
     * 결재 양식 수정
     * PATCH /v1/approval-forms/{id}
     */
    public function update(int $id, FormUpdateRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->formUpdate($id, $request->validated());
        }, __('message.updated'));
    }

    /**
     * 결재 양식 삭제
     * DELETE /v1/approval-forms/{id}
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->formDestroy($id);
        }, __('message.deleted'));
    }
}
