@extends('layouts.app')

@section('title', 'Applicant Details')
@section('heading', 'Applicant Details')

@section('content')
@include('recruitment.top-submenu')
    @php $progress = app(\App\Services\Recruitment\ReferralProgressService::class)->build($applicant); @endphp

    <x-card class="mb-4">
        <div class="mb-6 grid gap-3 md:grid-cols-2 lg:grid-cols-3">
            <div>
                <p class="text-xs uppercase text-slate-400">Name</p>
                <p class="text-sm font-semibold text-slate-900">{{ $applicant->name }}</p>
            </div>
            <div>
                <p class="text-xs uppercase text-slate-400">Applied for</p>
                <p class="text-sm text-slate-700">{{ $applicant->jobPosting?->title ?? '—' }}</p>
            </div>
            <div>
                <p class="text-xs uppercase text-slate-400">Source</p>
                <p class="text-sm text-slate-700">
                    @if($applicant->isReferral())
                        Employee referral
                    @elseif($applicant->isExternalApplication())
                        Careers page
                    @else
                        Direct / HR
                    @endif
                </p>
            </div>
            <div>
                <p class="text-xs uppercase text-slate-400">Email</p>
                <p class="text-sm text-slate-700">{{ $applicant->email ?: '—' }}</p>
            </div>
            <div>
                <p class="text-xs uppercase text-slate-400">Phone</p>
                <p class="text-sm text-slate-700">{{ $applicant->phone ?: '—' }}</p>
            </div>
            <div>
                <p class="text-xs uppercase text-slate-400">Referred by</p>
                <p class="text-sm text-slate-700">{{ $applicant->referredBy?->name ?? '—' }}</p>
            </div>
            <div>
                <p class="text-xs uppercase text-slate-400">Rating</p>
                <p class="text-sm text-slate-700">{{ $applicant->rating ? number_format($applicant->rating, 1).'/5' : '—' }}</p>
            </div>
            <div>
                <p class="text-xs uppercase text-slate-400">SLA due</p>
                <p class="text-sm text-slate-700">
                    @if($applicant->sla_due_at)
                        {{ $applicant->sla_due_at->format('d M Y H:i') }}
                        @if(app(\App\Services\Recruitment\RecruitmentSlaService::class)->isOverdue($applicant))
                            <span class="text-rose-600">(overdue)</span>
                        @endif
                    @else
                        —
                    @endif
                </p>
            </div>
            @if($applicant->isReferral() && $applicant->referral_bonus_amount)
            <div>
                <p class="text-xs uppercase text-slate-400">Referral bonus</p>
                <p class="text-sm text-slate-700">{{ number_format((float) $applicant->referral_bonus_amount, 2) }} ({{ ucfirst($applicant->referral_bonus_status ?? 'n/a') }})</p>
            </div>
            @endif
            <div>
                <p class="text-xs uppercase text-slate-400">Resume</p>
                @if($applicant->resume_path)
                    <a href="{{ route('applicants.resume.download', $applicant) }}" class="text-sm font-medium text-primary hover:underline">Download</a>
                @else
                    <p class="text-sm text-slate-700">—</p>
                @endif
            </div>
        </div>

        <div class="rounded-xl border border-slate-200 bg-slate-50/50 p-4">
            @include('recruitment.partials.progress-stepper', ['progress' => $progress, 'showPercent' => true])
        </div>

        <div class="mt-6">
            <p class="text-xs uppercase text-slate-400">Internal notes</p>
            <p class="mt-1 text-sm text-slate-700">{{ $applicant->notes ?: 'No notes added yet.' }}</p>
        </div>
    </x-card>

    @if($applicant->stageEvents->isNotEmpty())
        <x-card class="mb-4">
            <h3 class="text-sm font-semibold text-slate-900">Stage history</h3>
            <ul class="mt-4 space-y-3">
                @foreach($applicant->stageEvents as $event)
                    <li class="rounded-lg border border-slate-200 px-3 py-2">
                        <div class="flex flex-wrap items-center justify-between gap-2">
                            <p class="text-sm font-medium text-slate-800">
                                {{ $event->from_stage ? ucfirst($event->from_stage).' → ' : '' }}{{ ucfirst($event->to_stage) }}
                            </p>
                            <p class="text-xs text-slate-500">{{ $event->created_at?->format('d M Y, H:i') }} · {{ $event->changedBy?->name ?? 'System' }}</p>
                        </div>
                        @if($event->note_internal)<p class="mt-1 text-xs text-slate-600">HR: {{ $event->note_internal }}</p>@endif
                        @if($event->note_referrer)<p class="mt-1 text-xs text-teal-700">Referrer note: {{ $event->note_referrer }}</p>@endif
                    </li>
                @endforeach
            </ul>
        </x-card>
    @endif

    <x-card class="mb-4">
        <h3 class="mb-3 text-sm font-semibold text-slate-900">Interviews</h3>
        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">When</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Type</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Interviewer</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                </tr>
            </x-slot>
            @forelse($applicant->interviews as $interview)
                <tr>
                    <td class="px-4 py-3 text-sm">{{ $interview->scheduled_at?->format('d M Y, h:i A') ?? '—' }}</td>
                    <td class="px-4 py-3 text-sm">{{ ucfirst($interview->interview_type) }}</td>
                    <td class="px-4 py-3 text-sm">{{ $interview->interviewer?->name ?? '—' }}</td>
                    <td class="px-4 py-3 text-sm">{{ ucfirst($interview->status) }}</td>
                </tr>
            @empty
                <tr><td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500">No interviews scheduled.</td></tr>
            @endforelse
        </x-table>
    </x-card>

    <div class="flex gap-2">
        <a href="{{ route('applicants.index') }}" class="rounded-lg bg-slate-100 px-4 py-2 text-sm font-medium text-slate-700">Back</a>
        @if(auth()->user()->hasPermission('recruitment_edit'))
            <a href="{{ route('applicants.edit', $applicant) }}" class="rounded-lg bg-teal-700 px-4 py-2 text-sm font-medium text-white">Edit applicant</a>
        @endif
    </div>
@endsection
