<x-app-layout title="My shifts">
    <div class="space-y-6">
        <div class="flex flex-wrap items-start justify-between gap-4">
            <div>
                <h1 class="text-2xl font-semibold text-slate-900">My shifts</h1>
                <p class="mt-1 text-sm text-slate-500">
                    Your duty roster for
                    {{ $weekStart->format('d M Y') }} – {{ $weekStart->copy()->addDays(6)->format('d M Y') }}.
                </p>
            </div>
            <div class="flex flex-wrap gap-2">
                <a href="{{ route('care.my-shifts', ['week' => $prevWeek]) }}" class="btn-secondary">Previous</a>
                <a href="{{ route('care.my-shifts', ['week' => now()->startOfWeek(\Carbon\Carbon::MONDAY)->toDateString()]) }}" class="btn-secondary">This week</a>
                <a href="{{ route('care.my-shifts', ['week' => $nextWeek]) }}" class="btn-secondary">Next</a>
            </div>
        </div>

        @if ($todayEntries->isNotEmpty())
            <div class="rounded-2xl border border-teal-200 bg-teal-50 p-5">
                <h2 class="text-sm font-semibold uppercase tracking-wide text-teal-800">Today</h2>
                <ul class="mt-3 space-y-2">
                    @foreach ($todayEntries as $entry)
                        <li class="flex flex-wrap items-center justify-between gap-2 rounded-xl bg-white/80 px-4 py-3 text-sm">
                            <div>
                                <p class="font-semibold text-slate-900">{{ $entry->shift?->label ?? 'Shift' }}</p>
                                <p class="text-xs text-slate-500">
                                    {{ $entry->shift?->timeLabel() }}
                                    · {{ $entry->careUnit?->name ?? 'Unit' }}
                                    @if ($entry->careUnit?->department)
                                        ({{ $entry->careUnit->department->name }})
                                    @endif
                                </p>
                            </div>
                            <div class="flex flex-wrap items-center gap-2">
                                @if ($entry->careUnit && app(\App\Services\Care\CarePermissions::class)->can(
                                    auth()->user() ? app(\App\Services\Care\OrganizationResolver::class)->memberFor(auth()->user()) : null,
                                    'nursing.station.view'
                                ))
                                    <a href="{{ route('care.nursing.station', ['unit' => $entry->care_unit_id]) }}" class="rounded-lg bg-slate-900 px-2.5 py-1 text-xs font-semibold text-white hover:bg-slate-800">Open care unit</a>
                                @endif
                                <span class="rounded-full bg-teal-100 px-2.5 py-0.5 text-xs font-medium text-teal-800">
                                    {{ config('care.roster_entry_statuses.'.$entry->status, $entry->status) }}
                                </span>
                            </div>
                        </li>
                    @endforeach
                </ul>
            </div>
        @else
            <div class="rounded-2xl border border-slate-200 bg-white px-5 py-4 text-sm text-slate-500">
                No duty assigned for today.
            </div>
        @endif

        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                    <tr>
                        <th class="px-4 py-3">Day</th>
                        <th class="px-4 py-3">Shift</th>
                        <th class="px-4 py-3">Unit</th>
                        <th class="px-4 py-3">Status</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @foreach ($days as $day)
                        @php $dayEntries = $entriesByDate[$day->toDateString()] ?? []; @endphp
                        @forelse ($dayEntries as $entry)
                            <tr class="{{ $day->isToday() ? 'bg-teal-50/40' : '' }}">
                                <td class="px-4 py-3 whitespace-nowrap">
                                    <span class="font-medium text-slate-900">{{ $day->format('D') }}</span>
                                    <span class="text-slate-400">{{ $day->format('d M') }}</span>
                                    @if ($day->isToday())
                                        <span class="ml-1 text-[10px] font-semibold uppercase text-teal-700">Today</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3">
                                    <span class="mr-1 inline-block h-2 w-2 rounded-full" style="background: {{ $entry->shift?->color ?? '#94a3b8' }}"></span>
                                    {{ $entry->shift?->label ?? '—' }}
                                    <div class="text-xs text-slate-400">{{ $entry->shift?->timeLabel() }}</div>
                                </td>
                                <td class="px-4 py-3">
                                    {{ $entry->careUnit?->name ?? '—' }}
                                    @if ($entry->careUnit?->department)
                                        <div class="text-xs text-slate-400">{{ $entry->careUnit->department->name }}</div>
                                    @endif
                                </td>
                                <td class="px-4 py-3 capitalize text-slate-600">
                                    {{ config('care.roster_entry_statuses.'.$entry->status, $entry->status) }}
                                </td>
                            </tr>
                        @empty
                            <tr class="{{ $day->isToday() ? 'bg-teal-50/40' : '' }}">
                                <td class="px-4 py-3 whitespace-nowrap text-slate-500">
                                    <span class="font-medium text-slate-700">{{ $day->format('D') }}</span>
                                    {{ $day->format('d M') }}
                                </td>
                                <td class="px-4 py-3 text-slate-400" colspan="3">Off / not rostered</td>
                            </tr>
                        @endforelse
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
