<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\CalendarScheduleService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CalendarScheduleController extends Controller
{
    public function __construct(
        private readonly CalendarScheduleService $service
    ) {}

    /**
     * 일정 목록 조회
     */
    public function index(Request $request): JsonResponse
    {
        $request->validate([
            'year' => 'required|integer|min:2000|max:2100',
            'type' => 'nullable|string',
        ]);

        return ApiResponse::handle(
            fn () => $this->service->list(
                (int) $request->input('year'),
                $request->input('type')
            ),
            __('message.fetched')
        );
    }

    /**
     * 통계 조회
     */
    public function stats(Request $request): JsonResponse
    {
        $request->validate([
            'year' => 'required|integer|min:2000|max:2100',
        ]);

        return ApiResponse::handle(
            fn () => $this->service->stats((int) $request->input('year')),
            __('message.fetched')
        );
    }

    /**
     * 단건 조회
     */
    public function show(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->show($id),
            __('message.fetched')
        );
    }

    /**
     * 등록
     */
    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'start_date' => 'required|date',
            'end_date' => 'required|date|after_or_equal:start_date',
            'type' => 'required|string|in:public_holiday,temporary_holiday,substitute_holiday,tax_deadline,company_event',
            'is_recurring' => 'boolean',
            'memo' => 'nullable|string|max:500',
        ]);

        return ApiResponse::handle(
            fn () => $this->service->store($validated),
            __('message.created')
        );
    }

    /**
     * 수정
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'start_date' => 'required|date',
            'end_date' => 'required|date|after_or_equal:start_date',
            'type' => 'required|string|in:public_holiday,temporary_holiday,substitute_holiday,tax_deadline,company_event',
            'is_recurring' => 'boolean',
            'memo' => 'nullable|string|max:500',
        ]);

        return ApiResponse::handle(
            fn () => $this->service->update($id, $validated),
            __('message.updated')
        );
    }

    /**
     * 삭제
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->delete($id),
            __('message.deleted')
        );
    }

    /**
     * 대량 등록
     */
    public function bulkStore(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'schedules' => 'required|array|min:1',
            'schedules.*.name' => 'required|string|max:100',
            'schedules.*.start_date' => 'required|date',
            'schedules.*.end_date' => 'required|date|after_or_equal:schedules.*.start_date',
            'schedules.*.type' => 'required|string|in:public_holiday,temporary_holiday,substitute_holiday,tax_deadline,company_event',
            'schedules.*.is_recurring' => 'boolean',
            'schedules.*.memo' => 'nullable|string|max:500',
        ]);

        return ApiResponse::handle(
            fn () => $this->service->bulkStore($validated['schedules']),
            __('message.created')
        );
    }
}
