@extends('layouts.admin')

@section('title', 'Profilim')
@section('page-title', 'Profilim')
@section('page-subtitle', 'Hesap bilgilerinizi ve tercihlerinizi yönetin')

@section('content')
<div class="mt-4 grid grid-cols-1 lg:grid-cols-3 gap-5">

    {{-- Sol: Avatar + Özet --}}
    <div class="space-y-5">

        {{-- Avatar Kartı --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 text-center">
            <div class="relative inline-block mb-4">
                @if($user->avatar)
                    <img src="{{ Storage::url($user->avatar) }}" alt="{{ $user->name }}"
                         class="w-24 h-24 rounded-full object-cover border-4 border-orange-100 mx-auto">
                @else
                    <div class="w-24 h-24 rounded-full bg-primary flex items-center justify-center text-white text-3xl font-bold mx-auto border-4 border-orange-100">
                        {{ strtoupper(substr($user->name, 0, 1)) }}
                    </div>
                @endif
            </div>
            <h2 class="font-bold text-gray-800 text-base">{{ $user->name }}</h2>
            <p class="text-xs text-gray-400 mt-0.5">{{ $user->getRoleNames()->first() }}</p>
            @if($user->bio)
            <p class="text-sm text-gray-500 mt-3 leading-relaxed">{{ $user->bio }}</p>
            @endif

            {{-- Avatar Yükle --}}
            <div class="mt-5 pt-4 border-t border-gray-100">
                <form method="POST" action="{{ route('admin.profil.avatar') }}" enctype="multipart/form-data">
                    @csrf
                    <label class="block text-xs font-medium text-gray-600 mb-2">Fotoğraf Değiştir</label>
                    <input type="file" name="avatar" id="avatarInput" accept="image/*" class="hidden"
                           onchange="this.form.submit()">
                    <button type="button" onclick="document.getElementById('avatarInput').click()"
                            class="w-full border border-gray-300 text-gray-600 px-3 py-2 rounded-lg text-xs hover:bg-gray-50 transition-colors">
                        Fotoğraf Seç & Yükle
                    </button>
                    @error('avatar')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                </form>

                @if($user->avatar)
                <form method="POST" action="{{ route('admin.profil.avatar.remove') }}" class="mt-2">
                    @csrf
                    @method('DELETE')
                    <button type="submit" onclick="return confirm('Fotoğrafı kaldırmak istediğinizden emin misiniz?')"
                            class="w-full border border-red-200 text-red-500 px-3 py-2 rounded-lg text-xs hover:bg-red-50 transition-colors">
                        Fotoğrafı Kaldır
                    </button>
                </form>
                @endif
            </div>
        </div>

        {{-- Hesap Bilgileri --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-3">Hesap Bilgileri</h3>
            <div class="space-y-2 text-sm">
                <div class="flex justify-between">
                    <span class="text-gray-400">Üyelik</span>
                    <span class="text-gray-700">{{ $user->created_at->format('d.m.Y') }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-400">Rol</span>
                    <span class="text-gray-700">{{ $user->getRoleNames()->first() ?? '—' }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-400">2FA</span>
                    <span class="{{ $user->two_factor_confirmed_at ? 'text-green-600' : 'text-gray-400' }}">
                        {{ $user->two_factor_confirmed_at ? 'Aktif' : 'Pasif' }}
                    </span>
                </div>
            </div>
        </div>
    </div>

    {{-- Sağ: Formlar --}}
    <div class="lg:col-span-2 space-y-5">

        {{-- Profil Bilgileri Formu --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <h3 class="text-base font-semibold text-gray-800 mb-5">Profil Bilgileri</h3>
            <form method="POST" action="{{ route('admin.profil.update') }}" class="space-y-4">
                @csrf
                @method('PUT')

                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Ad Soyad <span class="text-red-500">*</span></label>
                        <input type="text" name="name" value="{{ old('name', $user->name) }}"
                               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 @error('name') border-red-400 @enderror">
                        @error('name')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">E-posta <span class="text-red-500">*</span></label>
                        <input type="email" name="email" value="{{ old('email', $user->email) }}"
                               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 @error('email') border-red-400 @enderror">
                        @error('email')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Kişisel Not / Biyografi</label>
                    <textarea name="bio" rows="3"
                              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 resize-none @error('bio') border-red-400 @enderror"
                              placeholder="Kendiniz hakkında kısa bir not...">{{ old('bio', $user->bio) }}</textarea>
                    <p class="text-xs text-gray-400 mt-1">Maksimum 500 karakter</p>
                    @error('bio')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                </div>

                <div class="flex justify-end">
                    <button type="submit" class="bg-primary text-white px-5 py-2 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors">
                        Bilgileri Kaydet
                    </button>
                </div>
            </form>
        </div>

        {{-- Şifre Değiştirme Formu --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6" id="password">
            <h3 class="text-base font-semibold text-gray-800 mb-5">Şifre Değiştir</h3>
            <form method="POST" action="{{ route('admin.profil.password') }}" class="space-y-4">
                @csrf
                @method('PUT')

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Mevcut Şifre <span class="text-red-500">*</span></label>
                    <div class="relative">
                        <input type="password" name="current_password" id="currentPasswordField"
                               class="w-full border border-gray-300 rounded-lg px-3 py-2 pr-10 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400 @error('current_password') border-red-400 @enderror">
                        <button type="button" onclick="togglePwd('currentPasswordField', this)"
                                class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600">
                            <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 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                        </button>
                    </div>
                    @error('current_password')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                </div>

                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Yeni Şifre <span class="text-red-500">*</span></label>
                        <div class="relative">
                            <input type="password" name="password" id="newPasswordField"
                                   oninput="updateProfilStrength(this.value); checkPasswordMatch()"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 pr-10 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400 @error('password') border-red-400 @enderror">
                            <button type="button" onclick="togglePwd('newPasswordField', this)"
                                    class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600">
                                <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 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                            </button>
                        </div>
                        {{-- Güvenlik Göstergesi (bilgi amaçlı) --}}
                        <div id="profilStrengthBar" class="hidden mt-2">
                            <div class="flex gap-1 mb-1">
                                <div id="profilBar1" class="h-1.5 flex-1 rounded-full bg-gray-200 transition-colors"></div>
                                <div id="profilBar2" class="h-1.5 flex-1 rounded-full bg-gray-200 transition-colors"></div>
                                <div id="profilBar3" class="h-1.5 flex-1 rounded-full bg-gray-200 transition-colors"></div>
                            </div>
                            <p id="profilStrengthLabel" class="text-xs text-gray-400"></p>
                        </div>
                        @error('password')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Yeni Şifre Tekrar <span class="text-red-500">*</span></label>
                        <div class="relative">
                            <input type="password" name="password_confirmation" id="confirmPasswordField"
                                   oninput="checkPasswordMatch()"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 pr-10 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                            <button type="button" onclick="togglePwd('confirmPasswordField', this)"
                                    class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600">
                                <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 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                            </button>
                        </div>
                        <p id="passwordMatchMsg" class="hidden text-red-500 text-xs mt-1">Şifreler eşleşmiyor.</p>
                        <p id="passwordMatchOk" class="hidden text-green-600 text-xs mt-1">Şifreler eşleşiyor.</p>
                    </div>
                </div>

                <p class="text-xs text-gray-400">Şifre en az 8 karakter olmalıdır.</p>

                <div class="flex justify-end">
                    <button type="submit" id="passwordSubmitBtn" class="bg-gray-800 text-white px-5 py-2 rounded-lg text-sm font-medium hover:bg-gray-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed">
                        Şifreyi Güncelle
                    </button>
                </div>
            </form>
        </div>

        {{-- Google Takvim Bağlantısı --}}
        @php
            $calendarService = app(\App\Services\GoogleCalendarService::class);
            $googleConfigured = $calendarService->isEnabled();
        @endphp
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div class="flex items-center gap-3 mb-4">
                <div class="w-9 h-9 bg-blue-50 rounded-lg flex items-center justify-center flex-shrink-0">
                    <svg class="w-5 h-5 text-blue-500" viewBox="0 0 24 24" fill="currentColor">
                        <path d="M19.5 3h-2.25V1.5h-1.5V3h-7.5V1.5h-1.5V3H4.5A1.5 1.5 0 003 4.5v15A1.5 1.5 0 004.5 21h15a1.5 1.5 0 001.5-1.5v-15A1.5 1.5 0 0019.5 3zm0 16.5h-15V9h15v10.5zm0-12h-15V4.5h2.25V6h1.5V4.5h7.5V6h1.5V4.5h2.25V7.5z"/>
                    </svg>
                </div>
                <div>
                    <h3 class="text-base font-semibold text-gray-800">Google Takvim</h3>
                    <p class="text-xs text-gray-400">Görüşmelerinizi otomatik olarak Google Takvim'e ekleyin</p>
                </div>
            </div>

            @if(!$googleConfigured)
                <div class="bg-amber-50 border border-amber-200 rounded-lg p-4 text-sm text-amber-700">
                    <div class="flex items-start gap-2">
                        <svg class="w-4 h-4 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.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/></svg>
                        <span>Google Calendar entegrasyonu henüz yapılandırılmamış. Lütfen <a href="{{ route('admin.ayarlar.index') }}" class="font-semibold underline">Site Ayarları</a>'ndan Client ID ve Client Secret girin.</span>
                    </div>
                </div>
            @elseif($user->hasGoogleCalendar())
                <div class="flex items-center justify-between bg-green-50 border border-green-200 rounded-lg px-4 py-3 mb-4">
                    <div class="flex items-center gap-2">
                        <span class="w-2.5 h-2.5 bg-green-500 rounded-full"></span>
                        <span class="text-sm font-medium text-green-700">Google Takvim bağlı</span>
                    </div>
                    <form method="POST" action="{{ route('admin.profil.google.disconnect') }}">
                        @csrf
                        <button type="submit"
                                onclick="return confirm('Google Takvim bağlantısını kesmek istediğinizden emin misiniz?')"
                                class="text-xs text-red-500 hover:text-red-700 border border-red-200 hover:border-red-400 px-3 py-1.5 rounded-lg transition-colors">
                            Bağlantıyı Kes
                        </button>
                    </form>
                </div>
                <p class="text-xs text-gray-400">Bundan sonra oluşturduğunuz görüşmeler otomatik olarak Google Takvim'inize eklenecek.</p>
            @else
                <p class="text-sm text-gray-500 mb-4">Google hesabınızı bağlayarak görüşmelerinizin otomatik olarak takviminize eklenmesini sağlayın.</p>
                <a href="{{ route('admin.profil.google.connect') }}"
                   class="inline-flex items-center gap-2 bg-white border border-gray-300 text-gray-700 px-4 py-2.5 rounded-lg text-sm font-medium hover:bg-gray-50 hover:border-gray-400 transition-colors shadow-sm">
                    <svg class="w-4 h-4" viewBox="0 0 24 24">
                        <path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/>
                        <path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/>
                        <path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/>
                        <path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/>
                    </svg>
                    Google Hesabınla Bağlan
                </a>
            @endif
        </div>

    </div>
</div>

<script nonce="{{ app('csp-nonce') }}">
function togglePwd(fieldId, btn) {
    const field = document.getElementById(fieldId);
    const isPassword = field.type === 'password';
    field.type = isPassword ? 'text' : 'password';
    btn.innerHTML = isPassword
        ? '<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="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"/></svg>'
        : '<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 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>';
}

// ─── Şifre Eşleşme Kontrolü ──────────────────────────────────────────────────
function checkPasswordMatch() {
    var pwd  = document.getElementById('newPasswordField').value;
    var conf = document.getElementById('confirmPasswordField').value;
    var msg  = document.getElementById('passwordMatchMsg');
    var ok   = document.getElementById('passwordMatchOk');
    var btn  = document.getElementById('passwordSubmitBtn');

    if (!conf) {
        msg.classList.add('hidden');
        ok.classList.add('hidden');
        btn.disabled = false;
        return;
    }

    if (pwd !== conf) {
        msg.classList.remove('hidden');
        ok.classList.add('hidden');
        btn.disabled = true;
    } else if (pwd.length < 8) {
        msg.classList.add('hidden');
        ok.classList.add('hidden');
        btn.disabled = true;
    } else {
        msg.classList.add('hidden');
        ok.classList.remove('hidden');
        btn.disabled = false;
    }
}

// ─── Şifre Güvenlik Göstergesi (bilgi amaçlı) ────────────────────────────────
function updateProfilStrength(val) {
    var bar = document.getElementById('profilStrengthBar');
    var label = document.getElementById('profilStrengthLabel');
    var b1 = document.getElementById('profilBar1');
    var b2 = document.getElementById('profilBar2');
    var b3 = document.getElementById('profilBar3');
    if (!val) { bar.classList.add('hidden'); return; }
    bar.classList.remove('hidden');
    var score = 0;
    if (val.length >= 8) score++;
    if (/[A-Z]/.test(val) && /[a-z]/.test(val)) score++;
    if (/[0-9]/.test(val)) score++;
    if (/[^A-Za-z0-9]/.test(val)) score++;
    var result;
    if (score <= 1) result = { score: 1, label: 'Zayıf — daha güçlü bir şifre kullanın', color: 'red' };
    else if (score <= 3) result = { score: 2, label: 'Orta — büyük/küçük harf, rakam ve özel karakter ekleyin', color: 'yellow' };
    else result = { score: 3, label: 'Güçlü', color: 'green' };
    var colors = { red: 'bg-red-400', yellow: 'bg-yellow-400', green: 'bg-green-500' };
    var c = colors[result.color] || 'bg-gray-200';
    b1.className = 'h-1.5 flex-1 rounded-full transition-colors ' + c;
    b2.className = 'h-1.5 flex-1 rounded-full transition-colors ' + (result.score >= 2 ? c : 'bg-gray-200');
    b3.className = 'h-1.5 flex-1 rounded-full transition-colors ' + (result.score >= 3 ? c : 'bg-gray-200');
    var textColors = { red: 'text-red-500', yellow: 'text-yellow-600', green: 'text-green-600' };
    label.className = 'text-xs ' + (textColors[result.color] || 'text-gray-400');
    label.textContent = result.label;
}
</script>
@endsection

@push('scripts')
{{-- Google Takvim bölümü için ek bir şey gerekmez --}}
@endpush
