<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Leave\BalanceRequest;
use App\Http\Requests\Leave\IndexRequest;
use App\Http\Requests\Leave\RejectRequest;
use App\Http\Requests\Leave\StoreRequest;
use App\Http\Requests\Leave\UpdateRequest;
use App\Services\LeaveService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class LeaveController extends Controller
{
    public function __construct(private LeaveService $service) {}

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

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

    /**
     * 휴가 신청
     * POST /v1/leaves
     */
    public function store(StoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.leave.created'));
    }

    /**
     * 휴가 수정
     * PATCH /v1/leaves/{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/leaves/{id}
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroy($id);
        }, __('message.deleted'));
    }

    /**
     * 휴가 승인
     * POST /v1/leaves/{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.leave.approved'));
    }

    /**
     * 휴가 반려
     * POST /v1/leaves/{id}/reject
     */
    public function reject(int $id, RejectRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->reject($id, $request->input('reason'));
        }, __('message.leave.rejected'));
    }

    /**
     * 휴가 취소
     * POST /v1/leaves/{id}/cancel
     */
    public function cancel(int $id, Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->cancel($id, $request->input('reason'));
        }, __('message.leave.cancelled'));
    }

    /**
     * 전체 직원 휴가 사용현황 목록
     * GET /v1/leaves/balances
     */
    public function balances(Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getAllBalances($request->all());
        }, __('message.fetched'));
    }

    /**
     * 내 잔여 휴가 조회
     * GET /v1/leaves/balance
     */
    public function balance(Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getMyBalance($request->input('year'));
        }, __('message.fetched'));
    }

    /**
     * 특정 사용자 잔여 휴가 조회
     * GET /v1/leaves/balance/{userId}
     */
    public function userBalance(int $userId, Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($userId, $request) {
            return $this->service->getUserBalance($userId, $request->input('year'));
        }, __('message.fetched'));
    }

    /**
     * 잔여 휴가 설정
     * PUT /v1/leaves/balance
     */
    public function setBalance(BalanceRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $request->validated();

            return $this->service->setBalance(
                $data['user_id'],
                $data['year'],
                $data['total_days']
            );
        }, __('message.updated'));
    }

    /**
     * 휴가 부여 이력 목록
     * GET /v1/leaves/grants
     */
    public function grants(Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getGrants($request->all());
        }, __('message.fetched'));
    }

    /**
     * 휴가 부여
     * POST /v1/leaves/grants
     */
    public function storeGrant(Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->storeGrant($request->all());
        }, __('message.leave.granted'));
    }

    /**
     * 휴가 부여 삭제
     * DELETE /v1/leaves/grants/{id}
     */
    public function destroyGrant(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroyGrant($id);
        }, __('message.deleted'));
    }
}
