<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\HolidayResource;
use App\Models\Holiday;
use App\Services\CapacityService;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class HolidayController extends Controller
{
    public function __construct(protected CapacityService $capacityService) {}

    /**
     * List Holidays
     *
     * Retrieve holidays for a specific month or all holidays when no month is provided.
     *
     * @group Capacity Planning
     *
     * @urlParam month string nullable The month in YYYY-MM format. Example: 2026-02
     *
     * @response {
     *   "data": [
     *     {
     *       "id": "550e8400-e29b-41d4-a716-446655440000",
     *       "date": "2026-02-14",
     *       "name": "Company Holiday",
     *       "description": "Office closed"
     *     }
     *   ]
     * }
     */
    public function index(Request $request): JsonResponse
    {
        $data = $request->validate([
            'month' => 'nullable|date_format:Y-m',
        ]);

        $holidays = isset($data['month'])
            ? $this->capacityService->getHolidaysForMonth($data['month'])
            : Holiday::orderBy('date')->get();

        return $this->wrapResource(HolidayResource::collection($holidays));
    }

    /**
     * Create Holiday
     *
     * Add a holiday and clear cached capacity data for the related month.
     *
     * @group Capacity Planning
     *
     * @bodyParam date string required Date of the holiday. Example: 2026-02-14
     * @bodyParam name string required Name of the holiday. Example: Presidents' Day
     * @bodyParam description string nullable Optional description of the holiday.
     *
     * @response 201 {
     *   "data": {
     *     "id": "550e8400-e29b-41d4-a716-446655440000",
     *     "date": "2026-02-14",
     *     "name": "Presidents' Day",
     *     "description": "Office closed"
     *   }
     * }
     * @response 422 {"message":"A holiday already exists for this date.","errors":{"date":["A holiday already exists for this date."]}}
     */
    public function store(Request $request): JsonResponse
    {
        $data = $request->validate([
            'date' => 'required|date',
            'name' => 'required|string',
            'description' => 'nullable|string',
        ]);

        try {
            $holiday = Holiday::create($data);
            $this->capacityService->forgetCapacityCacheForMonth($holiday->date->format('Y-m'));

            return $this->wrapResource(new HolidayResource($holiday), 201);
        } catch (UniqueConstraintViolationException $e) {
            return response()->json([
                'message' => 'A holiday already exists for this date.',
                'errors' => [
                    'date' => ['A holiday already exists for this date.'],
                ],
            ], 422);
        }
    }

    /**
     * Delete Holiday
     *
     * Remove a holiday and clear affected capacity caches.
     *
     * @group Capacity Planning
     *
     * @urlParam id string required The holiday UUID. Example: 550e8400-e29b-41d4-a716-446655440000
     *
     * @response {
     *   "message": "Holiday deleted"
     * }
     */
    public function destroy(string $id): JsonResponse
    {
        $holiday = Holiday::find($id);

        if (! $holiday) {
            return response()->json(['message' => 'Holiday not found'], 404);
        }

        $month = $holiday->date->format('Y-m');
        $holiday->delete();
        $this->capacityService->forgetCapacityCacheForMonth($month);

        return response()->json(['message' => 'Holiday deleted']);
    }
}
