<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\CalendarService;
use Carbon\Carbon;
use Illuminate\Http\Request;

/**
 * CEO 대시보드 캘린더 컨트롤러
 */
class CalendarController extends Controller
{
    public function __construct(
        private readonly CalendarService $calendarService
    ) {}

    /**
     * 캘린더 일정 조회
     *
     * @param  Request  $request
     *                            - start_date: 조회 시작일 (Y-m-d, 기본: 이번 달 1일)
     *                            - end_date: 조회 종료일 (Y-m-d, 기본: 이번 달 말일)
     *                            - type: 일정 타입 필터 (schedule|order|construction|null=전체)
     *                            - department_filter: 부서 필터 (all|department|personal, 기본: all)
     */
    public function summary(Request $request)
    {
        $validated = $request->validate([
            'start_date' => 'nullable|date_format:Y-m-d',
            'end_date' => 'nullable|date_format:Y-m-d|after_or_equal:start_date',
            'type' => 'nullable|in:schedule,order,construction,other',
            'department_filter' => 'nullable|in:all,department,personal',
        ]);

        // 기본값 설정: 이번 달 전체
        $today = Carbon::today();
        $startDate = $validated['start_date'] ?? $today->copy()->startOfMonth()->format('Y-m-d');
        $endDate = $validated['end_date'] ?? $today->copy()->endOfMonth()->format('Y-m-d');
        $type = $validated['type'] ?? null;
        $departmentFilter = $validated['department_filter'] ?? 'all';

        return ApiResponse::handle(function () use ($startDate, $endDate, $type, $departmentFilter) {
            return $this->calendarService->getSchedules(
                $startDate,
                $endDate,
                $type,
                $departmentFilter
            );
        }, __('message.fetched'));
    }

    /**
     * 일정 등록
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:200',
            'description' => 'nullable|string|max:1000',
            'start_date' => 'required|date_format:Y-m-d',
            'end_date' => 'required|date_format:Y-m-d|after_or_equal:start_date',
            'start_time' => 'nullable|date_format:H:i',
            'end_time' => 'nullable|date_format:H:i',
            'is_all_day' => 'boolean',
            'color' => 'nullable|string|max:20',
        ]);

        return ApiResponse::handle(function () use ($validated) {
            return $this->calendarService->createSchedule($validated);
        }, __('message.created'));
    }

    /**
     * 일정 수정
     */
    public function update(Request $request, int $id)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:200',
            'description' => 'nullable|string|max:1000',
            'start_date' => 'required|date_format:Y-m-d',
            'end_date' => 'required|date_format:Y-m-d|after_or_equal:start_date',
            'start_time' => 'nullable|date_format:H:i',
            'end_time' => 'nullable|date_format:H:i',
            'is_all_day' => 'boolean',
            'color' => 'nullable|string|max:20',
        ]);

        return ApiResponse::handle(function () use ($id, $validated) {
            return $this->calendarService->updateSchedule($id, $validated);
        }, __('message.updated'));
    }

    /**
     * 일정 삭제
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->calendarService->deleteSchedule($id);
        }, __('message.deleted'));
    }
}
