<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Approval\LineIndexRequest;
use App\Http\Requests\Approval\LineStoreRequest;
use App\Http\Requests\Approval\LineUpdateRequest;
use App\Services\ApprovalService;
use Illuminate\Http\JsonResponse;

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

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

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

    /**
     * 결재선 생성
     * POST /v1/approval-lines
     */
    public function store(LineStoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->lineStore($request->validated());
        }, __('message.approval.line_created'));
    }

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

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