@extends('layouts.app')

@section('title', 'Interview Schedule')
@section('heading', 'Interview Schedule')

@section('content')
    @include('recruitment.top-submenu')
    <div class="space-y-6">
        @unless($isHrSchedule)
            <div>
                <h1 class="text-lg font-semibold text-slate-900">Interview Schedule</h1>
                <p class="mt-1 text-sm text-slate-500">Interviews assigned to you.</p>
            </div>
        @endunless

        @if($isHrSchedule && $followUpsDue->isNotEmpty())
            <x-card>
                <p class="mb-3 text-sm font-semibold text-amber-900">Follow-ups due</p>
                <p class="mb-4 text-xs text-slate-500">Scheduled interviews with a follow-up time on or before today.</p>
                <div class="space-y-3">
                    @foreach($followUpsDue as $item)
                        <div class="flex flex-wrap items-center justify-between gap-2 rounded-lg border border-amber-200 bg-amber-50/50 p-3">
                            <div>
                                <p class="text-sm font-semibold text-slate-900">{{ $item->applicant?->name ?? 'Applicant' }}</p>
                                <p class="text-xs text-slate-500">{{ $item->applicant?->jobPosting?->title ?? '—' }}</p>
                                <p class="mt-1 text-xs text-slate-600">Follow-up: {{ $item->follow_up_at?->format('Y-m-d h:i A') ?? '—' }}</p>
                                <p class="mt-1 text-xs text-slate-600">Interviewer: {{ $item->interviewer?->name ?? '—' }}</p>
                            </div>
                            @if(auth()->user()->hasPermission('recruitment_edit'))
                                <a href="{{ route('interviews.edit', $item) }}" class="text-sm font-medium text-primary hover:underline">Update</a>
                            @endif
                        </div>
                    @endforeach
                </div>
            </x-card>
        @endif

        <x-card>
            <div class="grid gap-6 lg:grid-cols-2">
                <div class="rounded-xl border border-slate-200 p-4">
                    <p class="mb-1 text-sm font-semibold text-slate-900">Today - {{ now()->format('F d, Y') }}</p>
                    <p class="mb-3 text-xs text-slate-500">{{ $isHrSchedule ? 'Manage upcoming interviews' : 'Your interviews today' }}</p>
                    <div class="space-y-3">
                        @forelse($todayInterviews as $item)
                            <div class="rounded-lg border border-slate-200 p-3">
                                <p class="text-sm font-semibold text-slate-900">{{ $item->applicant?->name ?? 'Applicant' }}</p>
                                <p class="text-xs text-slate-500">{{ $item->applicant?->jobPosting?->title ?? '—' }}</p>
                                <p class="mt-1 text-xs text-slate-500">{{ ucfirst($item->interview_type) }} · {{ $item->scheduled_at?->format('h:i A') }}</p>
                                @if($isHrSchedule)
                                    <p class="mt-1 text-xs text-slate-600">Interviewer: {{ $item->interviewer?->name ?? '—' }}</p>
                                @endif
                                @if($item->meeting_link)
                                    <a href="{{ $item->meeting_link }}" target="_blank" rel="noopener noreferrer" class="mt-1 inline-block text-xs font-medium text-primary hover:underline">Join meeting</a>
                                @endif
                                @if($isHrSchedule && $item->follow_up_at)
                                    <p class="mt-1 text-xs text-amber-800">Next follow-up: {{ $item->follow_up_at->format('M j, h:i A') }}</p>
                                @endif
                                @if($isHrSchedule && auth()->user()->hasPermission('recruitment_edit'))
                                    <a href="{{ route('interviews.edit', $item) }}" class="mt-2 inline-block text-xs font-medium text-primary hover:underline">Edit schedule</a>
                                @endif
                            </div>
                        @empty
                            <p class="text-sm text-slate-500">No interviews scheduled for today.</p>
                        @endforelse
                    </div>
                </div>
                <div class="rounded-xl border border-slate-200 p-4">
                    <p class="mb-1 text-sm font-semibold text-slate-900">{{ $isHrSchedule ? 'Upcoming (next 14 days)' : 'Upcoming' }}</p>
                    <p class="mb-3 text-xs text-slate-500">After today</p>
                    <div class="space-y-3">
                        @forelse($upcomingInterviews as $item)
                            <div class="rounded-lg border border-slate-200 p-3">
                                <p class="text-sm font-semibold text-slate-900">{{ $item->applicant?->name ?? 'Applicant' }}</p>
                                <p class="text-xs text-slate-500">{{ $item->applicant?->jobPosting?->title ?? '—' }}</p>
                                <p class="mt-1 text-xs text-slate-500">{{ $item->scheduled_at?->format('Y-m-d h:i A') ?? '—' }}</p>
                                @if($isHrSchedule)
                                    <p class="mt-1 text-xs text-slate-600">Interviewer: {{ $item->interviewer?->name ?? '—' }}</p>
                                @endif
                                @if($item->meeting_link)
                                    <a href="{{ $item->meeting_link }}" target="_blank" rel="noopener noreferrer" class="mt-1 inline-block text-xs font-medium text-primary hover:underline">Join meeting</a>
                                @endif
                                @if($isHrSchedule && $item->follow_up_at)
                                    <p class="mt-1 text-xs text-slate-600">Follow-up: {{ $item->follow_up_at->format('M j, h:i A') }}</p>
                                @endif
                                @if($isHrSchedule && auth()->user()->hasPermission('recruitment_edit'))
                                    <a href="{{ route('interviews.edit', $item) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                                @endif
                            </div>
                        @empty
                            <p class="text-sm text-slate-500">No upcoming interviews.</p>
                        @endforelse
                    </div>
                </div>
            </div>
        </x-card>

        <x-card>
            @if($isHrSchedule && auth()->user()->hasPermission('recruitment_create'))
            <div class="mb-4 flex justify-end">
                <a href="{{ route('interviews.create') }}" class="inline-flex w-full justify-center rounded-lg bg-teal-700 px-4 py-2 text-sm font-medium text-white hover:bg-teal-600 sm:w-auto">+ Schedule Interview</a>
            </div>
            @endif
            <p class="mb-3 text-sm font-semibold text-slate-900">Completed Interviews</p>
            <x-table>
                <x-slot name="head">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Candidate</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Position</th>
                        @if($isHrSchedule)
                            <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Interviewer</th>
                        @endif
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                        <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                    </tr>
                </x-slot>
                @forelse($completedInterviews as $interview)
                    <tr>
                        <td class="px-4 py-3 text-sm text-slate-700">{{ $interview->applicant?->name ?? '—' }}</td>
                        <td class="px-4 py-3 text-sm text-slate-700">{{ $interview->applicant?->jobPosting?->title ?? '—' }}</td>
                        @if($isHrSchedule)
                            <td class="px-4 py-3 text-sm text-slate-700">{{ $interview->interviewer?->name ?? '—' }}</td>
                        @endif
                        <td class="px-4 py-3 text-sm text-slate-700">{{ $interview->scheduled_at?->format('Y-m-d') ?? '—' }}</td>
                        <td class="px-4 py-3 text-sm">
                            <span class="rounded-full bg-emerald-100 px-2 py-0.5 text-xs text-emerald-700">Completed</span>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="{{ $isHrSchedule ? 5 : 4 }}" class="px-4 py-8 text-center text-sm text-slate-500">No completed interviews yet.</td></tr>
                @endforelse
            </x-table>
            @if($completedInterviews->hasPages())<div class="mt-4">{{ $completedInterviews->links() }}</div>@endif
        </x-card>
    </div>
@endsection
