@extends('layouts.admin')

@section('title', 'Görüşme Detayı')
@section('page-title', $gorusme->title)
@section('page-subtitle', $gorusme->scheduled_at ? $gorusme->scheduled_at->format('d.m.Y H:i') : 'Tarih belirsiz')

@section('content')
<div class="mt-4 mb-3">
    <a href="{{ route('admin.gorusmeler.index') }}"
       class="inline-flex items-center gap-1.5 text-sm text-gray-500 hover:text-primary transition-colors">
        <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="M15 19l-7-7 7-7"/></svg>
        Görüşmelere Dön
    </a>
</div>

{{-- Geçmiş kalmış görüşme uyarısı --}}
@php
    $isPastAndPending = $gorusme->scheduled_at
        && $gorusme->scheduled_at->isPast()
        && in_array($gorusme->status, ['scheduled', 'rescheduled']);
@endphp
@if($isPastAndPending)
<div class="mb-4 flex items-start gap-3 bg-red-50 border border-red-300 rounded-xl px-5 py-4">
    <svg class="w-5 h-5 text-red-500 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/>
    </svg>
    <div>
        <p class="font-semibold text-red-700 text-sm">Bu görüşmenin tarihi geçmiş!</p>
        <p class="text-red-600 text-xs mt-0.5">
            {{ $gorusme->scheduled_at->format('d.m.Y H:i') }} tarihinde planlanmıştı ancak henüz sonuçlandırılmadı.
            Lütfen durumu güncelleyin.
        </p>
    </div>
    <a href="{{ route('admin.gorusmeler.edit', $gorusme) }}"
       class="ml-auto flex-shrink-0 bg-red-600 text-white px-3 py-1.5 rounded-lg text-xs font-medium hover:bg-red-700 transition-colors whitespace-nowrap">
        Durumu Güncelle
    </a>
</div>
@endif
<div class="grid grid-cols-1 lg:grid-cols-3 gap-5">

    {{-- Sol: Detaylar --}}
    <div class="lg:col-span-2 space-y-5">

        {{-- Görüşme Bilgileri --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            @php
                $colors = ['scheduled' => 'blue', 'completed' => 'green', 'cancelled' => 'red', 'rescheduled' => 'yellow'];
                $color = $colors[$gorusme->status] ?? 'gray';
            @endphp
            <div class="flex items-start justify-between mb-5">
                <div>
                    <h2 class="font-bold text-gray-800 text-lg">{{ $gorusme->title }}</h2>
                    <span class="inline-block mt-1 text-xs px-2 py-1 rounded-full font-medium bg-{{ $color }}-100 text-{{ $color }}-700">
                        {{ $gorusme->status_label }}
                    </span>
                </div>
                <div class="flex gap-2">
                    <a href="{{ route('admin.gorusmeler.edit', $gorusme) }}"
                       class="border border-gray-300 text-gray-600 px-3 py-1.5 rounded-lg text-sm hover:bg-gray-50 transition-colors">
                        Düzenle
                    </a>
                    <form method="POST" action="{{ route('admin.gorusmeler.destroy', $gorusme) }}"
                          onsubmit="return confirm('Bu görüşmeyi silmek istediğinizden emin misiniz?')">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="border border-red-200 text-red-500 px-3 py-1.5 rounded-lg text-sm hover:bg-red-50 transition-colors">
                            Sil
                        </button>
                    </form>
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4 text-sm">
                <div>
                    <p class="text-xs text-gray-400 mb-0.5">Tarih & Saat</p>
                    <p class="font-medium text-gray-700">{{ $gorusme->scheduled_at->format('d.m.Y H:i') }}</p>
                </div>
                <div>
                    <p class="text-xs text-gray-400 mb-0.5">Süre</p>
                    <p class="font-medium text-gray-700">{{ $gorusme->duration_minutes }} dakika</p>
                </div>
                <div>
                    <p class="text-xs text-gray-400 mb-0.5">Görüşmeci</p>
                    <p class="font-medium text-gray-700">{{ $gorusme->interviewer->name ?? '—' }}</p>
                </div>
                @if($gorusme->location)
                <div>
                    <p class="text-xs text-gray-400 mb-0.5">Konum</p>
                    <p class="font-medium text-gray-700">{{ $gorusme->location }}</p>
                </div>
                @endif
                @if($gorusme->meeting_link)
                <div class="col-span-2">
                    <p class="text-xs text-gray-400 mb-0.5">Online Toplantı</p>
                    @php
                        $meetingLinkSafe = (str_starts_with($gorusme->meeting_link, 'https://') || str_starts_with($gorusme->meeting_link, 'http://'))
                            ? $gorusme->meeting_link : null;
                    @endphp
                    @if($meetingLinkSafe)
                    <a href="{{ $meetingLinkSafe }}" target="_blank" rel="noopener noreferrer"
                       class="text-primary hover:underline text-sm break-all">{{ $gorusme->meeting_link }}</a>
                    @else
                    <span class="text-gray-400 text-sm">Geçersiz link</span>
                    @endif
                </div>
                @endif
                @if($gorusme->google_event_id)
                <div>
                    <p class="text-xs text-gray-400 mb-0.5">Google Calendar</p>
                    <span class="text-xs text-green-600 bg-green-50 px-2 py-0.5 rounded-full">Senkronize</span>
                </div>
                @endif
            </div>

            @if($gorusme->notes)
            <div class="mt-4 pt-4 border-t border-gray-100">
                <p class="text-xs text-gray-400 mb-1">Notlar</p>
                <p class="text-sm text-gray-700 whitespace-pre-line">{{ $gorusme->notes }}</p>
            </div>
            @endif
        </div>

    </div>

    {{-- Sağ: Başvuran Bilgileri --}}
    <div class="space-y-5">
        @php
            $candidateName  = $gorusme->application?->full_name ?? $gorusme->candidate_name;
            $candidateEmail = $gorusme->application?->email     ?? $gorusme->candidate_email;
            $candidatePhone = $gorusme->application?->phone     ?? null;
            $candidatePos   = $gorusme->application?->jobPosting?->title ?? null;
        @endphp
        @if($candidateName || $candidateEmail)
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5">
            <h3 class="font-semibold text-gray-700 mb-4 text-sm flex items-center gap-2">
                Görüşülecek Kişi
                @if(!$gorusme->application)
                    <span class="text-xs font-normal text-gray-400 bg-gray-100 px-2 py-0.5 rounded-full">Manuel</span>
                @endif
            </h3>
            <div class="space-y-3 text-sm">
                @if($candidateName)
                <div>
                    <p class="text-xs text-gray-400">Ad Soyad</p>
                    <p class="font-medium text-gray-800">{{ $candidateName }}</p>
                </div>
                @endif
                @if($candidateEmail)
                <div>
                    <p class="text-xs text-gray-400">E-posta</p>
                    <a href="mailto:{{ $candidateEmail }}" class="text-primary hover:underline">
                        {{ $candidateEmail }}
                    </a>
                </div>
                @endif
                @if($candidatePhone)
                <div>
                    <p class="text-xs text-gray-400">Telefon</p>
                    <p class="text-gray-700">{{ $candidatePhone }}</p>
                </div>
                @endif
                @if($candidatePos)
                <div>
                    <p class="text-xs text-gray-400">Pozisyon</p>
                    <p class="text-gray-700">{{ $candidatePos }}</p>
                </div>
                @endif
            </div>
            @if($gorusme->application)
            <div class="mt-4 pt-4 border-t border-gray-100">
                <a href="{{ route('admin.basvurular.show', $gorusme->application) }}"
                   class="text-sm text-primary hover:underline">Başvuruyu Görüntüle →</a>
            </div>
            @endif
        </div>
        @endif

        {{-- Hızlı Durum Güncelle --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5">
            <h3 class="font-semibold text-gray-700 mb-3 text-sm">Durumu Güncelle</h3>
            <form method="POST" action="{{ route('admin.gorusmeler.update', $gorusme) }}">
                @csrf
                @method('PUT')
                <input type="hidden" name="title" value="{{ $gorusme->title }}">
                <input type="hidden" name="scheduled_at" value="{{ $gorusme->scheduled_at ? $gorusme->scheduled_at->format('Y-m-d\TH:i') : '' }}">
                <input type="hidden" name="duration_minutes" value="{{ $gorusme->duration_minutes }}">
                <input type="hidden" name="location" value="{{ $gorusme->location }}">
                <input type="hidden" name="meeting_link" value="{{ $gorusme->meeting_link }}">
                <input type="hidden" name="notes" value="{{ $gorusme->notes }}">
                <input type="hidden" name="interviewer_id" value="{{ $gorusme->interviewer_id }}">
                <select name="status" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400 mb-3">
                    <option value="scheduled" {{ $gorusme->status === 'scheduled' ? 'selected' : '' }}>Planlandı</option>
                    <option value="completed" {{ $gorusme->status === 'completed' ? 'selected' : '' }}>Tamamlandı</option>
                    <option value="cancelled" {{ $gorusme->status === 'cancelled' ? 'selected' : '' }}>İptal Edildi</option>
                    <option value="rescheduled" {{ $gorusme->status === 'rescheduled' ? 'selected' : '' }}>Yeniden Planlandı</option>
                </select>
                <button type="submit" class="w-full bg-primary text-white py-2 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors">
                    Güncelle
                </button>
            </form>
        </div>
    </div>

</div>
@endsection