{{-- 대시보드 달력 그리드 (HTMX로 로드) --}}
@php
    use Carbon\Carbon;
    use App\Models\System\Schedule;

    $firstDay = Carbon::create($year, $month, 1);
    $lastDay = $firstDay->copy()->endOfMonth();
    $startOfWeek = $firstDay->copy()->startOfWeek(Carbon::SUNDAY);
    $endOfWeek = $lastDay->copy()->endOfWeek(Carbon::SATURDAY);

    $today = Carbon::today();
    $prevMonth = $firstDay->copy()->subMonth();
    $nextMonth = $firstDay->copy()->addMonth();
@endphp

{{-- 달력 헤더 --}}
<div class="flex items-center justify-between mb-4">
    <div class="flex items-center gap-3">
        <button type="button"
            hx-get="{{ route('dashboard.calendar', ['year' => $prevMonth->year, 'month' => $prevMonth->month]) }}"
            hx-target="#calendar-container"
            hx-swap="innerHTML"
            class="p-2 hover:bg-gray-100 rounded-lg transition-colors">
            <svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
            </svg>
        </button>

        <h2 class="text-xl font-bold text-gray-800">{{ $year }}년 {{ $month }}월</h2>

        <button type="button"
            hx-get="{{ route('dashboard.calendar', ['year' => $nextMonth->year, 'month' => $nextMonth->month]) }}"
            hx-target="#calendar-container"
            hx-swap="innerHTML"
            class="p-2 hover:bg-gray-100 rounded-lg transition-colors">
            <svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
            </svg>
        </button>

        @if(!$today->isSameMonth($firstDay))
        <button type="button"
            hx-get="{{ route('dashboard.calendar', ['year' => $today->year, 'month' => $today->month]) }}"
            hx-target="#calendar-container"
            hx-swap="innerHTML"
            class="ml-2 px-3 py-1 text-sm bg-gray-100 hover:bg-gray-200 text-gray-600 rounded-lg transition-colors">
            오늘
        </button>
        @endif
    </div>

    <button type="button" onclick="openCreateModal()"
       class="inline-flex items-center gap-2 px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg transition-colors text-sm">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
        </svg>
        일정 추가
    </button>
</div>

{{-- 달력 그리드 --}}
<div class="overflow-x-auto">
    <table class="w-full border-collapse">
        <thead>
            <tr class="bg-gray-50">
                <th class="px-2 py-3 text-center text-sm font-semibold text-red-500 border-b w-[14.28%]">일</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">월</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">화</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">수</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">목</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">금</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-blue-500 border-b w-[14.28%]">토</th>
            </tr>
        </thead>
        <tbody>
            @php
                $currentDate = $startOfWeek->copy();
            @endphp

            @while($currentDate <= $endOfWeek)
            <tr>
                @for($i = 0; $i < 7; $i++)
                    @php
                        $dateKey = $currentDate->format('Y-m-d');
                        $isCurrentMonth = $currentDate->month === $month;
                        $isToday = $currentDate->isSameDay($today);
                        $isSunday = $currentDate->dayOfWeek === Carbon::SUNDAY;
                        $isSaturday = $currentDate->dayOfWeek === Carbon::SATURDAY;
                        $daySchedules = $calendarData[$dateKey] ?? collect();
                        $holidayName = $holidayMap[$dateKey] ?? null;
                        $isHoliday = $holidayName !== null;
                    @endphp

                    <td class="border border-gray-100 align-top {{ !$isCurrentMonth ? 'bg-gray-50/50' : '' }}">
                        <div class="p-1" style="min-height: 7.5rem;">
                            {{-- 날짜 헤더 --}}
                            <div class="flex items-center justify-between mb-1">
                                <div class="flex items-center gap-1.5">
                                    <span class="{{ $isHoliday && $isCurrentMonth ? 'text-base font-bold' : 'text-sm font-medium' }}
                                        {{ !$isCurrentMonth ? 'text-gray-300' : '' }}
                                        {{ $isCurrentMonth && ($isSunday || $isHoliday) ? 'text-red-500' : '' }}
                                        {{ $isCurrentMonth && $isSaturday && !$isHoliday ? 'text-blue-500' : '' }}
                                        {{ $isCurrentMonth && !$isSunday && !$isSaturday && !$isHoliday ? 'text-gray-700' : '' }}
                                        {{ $isToday ? 'bg-emerald-500 !text-white rounded-full w-6 h-6 flex items-center justify-center' : '' }}
                                    ">
                                        {{ $currentDate->day }}
                                    </span>
                                    @if($isHoliday && $isCurrentMonth)
                                        <span class="text-xs text-red-500 font-bold truncate max-w-[80px]" title="{{ $holidayName }}">{{ $holidayName }}</span>
                                    @endif
                                </div>

                                @if($isCurrentMonth)
                                <button type="button" onclick="openCreateModal('{{ $dateKey }}')"
                                   class="text-gray-300 hover:text-emerald-600 hover:bg-emerald-50 rounded transition-colors p-0.5"
                                   title="{{ $currentDate->format('m/d') }} 일정 추가">
                                    <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                                    </svg>
                                </button>
                                @endif
                            </div>

                            {{-- 일정 목록 --}}
                            <div class="space-y-0.5">
                                @foreach($daySchedules as $schedule)
                                    <button type="button" onclick="openEditModal({{ $schedule->id }})"
                                       class="w-full text-left px-1.5 py-0.5 rounded text-xs border cursor-pointer hover:opacity-80 transition-opacity flex items-center justify-between"
                                       style="{{ $schedule->type_style }}"
                                       title="[{{ $schedule->type_label }}] {{ $schedule->title }}{{ !$schedule->is_all_day && $schedule->start_time ? ' ('.$schedule->start_time.')' : '' }}{{ $schedule->files_count ? ' (첨부 '.$schedule->files_count.'개)' : '' }}">
                                        <span class="truncate min-w-0">
                                            <span class="font-bold">[{{ $schedule->type_label }}]</span>
                                            @if(!$schedule->is_all_day && $schedule->start_time)
                                                <span class="font-medium">{{ \Carbon\Carbon::parse($schedule->start_time)->format('H:i') }}</span>
                                            @endif
                                            {{ $schedule->title }}
                                        </span>
                                        @if($schedule->files_count > 0)
                                            <svg class="w-3 h-3 flex-shrink-0 opacity-60 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"/>
                                            </svg>
                                        @endif
                                    </button>
                                @endforeach
                            </div>
                        </div>
                    </td>

                    @php
                        $currentDate->addDay();
                    @endphp
                @endfor
            </tr>
            @endwhile
        </tbody>
    </table>
</div>
