<x-app-layout>

    <x-module-hero
        title="Security Settings"
        description="Manage your password, two-factor authentication, and login history"
        icon="fa-lock"
    >
        <x-slot name="tabs">
            <a href="{{ route('profile.edit') }}"
               class="module-hero-tab {{ request()->routeIs('profile.edit') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-user text-sm"></i> Profile
            </a>
            <a href="{{ route('settings.security') }}"
               class="module-hero-tab {{ request()->routeIs('settings.security') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-lock text-sm"></i> Security
            </a>
            @role('Super Manager')
            <a href="{{ route('otp.settings') }}"
               class="module-hero-tab {{ request()->routeIs('otp.settings') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-shield-alt text-sm"></i> OTP Settings
            </a>
            @endrole
            <a href="{{ route('settings.sessions') }}"
               class="module-hero-tab {{ request()->routeIs('settings.sessions') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-desktop text-sm"></i> Sessions
            </a>
        </x-slot>
    </x-module-hero>

    <div class="p-6 space-y-6">

        @if(session('success'))
            <div class="p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-3">
                <i class="fas fa-check-circle text-xl"></i>{{ session('success') }}
            </div>
        @endif

        @if(session('error'))
            <div class="p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-3">
                <i class="fas fa-exclamation-circle text-xl"></i>{{ session('error') }}
            </div>
        @endif

        {{-- Change Password --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
            <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-900 flex items-center gap-2">
                    <i class="fas fa-lock text-purple-600"></i> Change Password
                </h3>
            </div>
            <div class="p-6">
                <p class="text-sm text-gray-600 mb-6">
                    Use a strong password of at least 8 characters. A mix of letters, numbers, and symbols is recommended.
                </p>
                <form method="POST"
                      action="{{ route('password.update') }}"
                      x-data="{ loading: false, showCurrent: false, showNew: false, showConfirm: false }"
                      @submit="loading = true">
                    @csrf
                    @method('PUT')

                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
                        {{-- Current Password --}}
                        <div>
                            <label for="current_password" class="block text-sm font-medium text-gray-700 mb-2">
                                Current Password <span class="text-red-500">*</span>
                            </label>
                            <div class="relative">
                                <input :type="showCurrent ? 'text' : 'password'"
                                       id="current_password" name="current_password"
                                       required autocomplete="current-password"
                                       class="w-full px-4 py-3 pr-12 border-2 border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition @error('current_password') border-red-500 @enderror">
                                <button type="button" @click="showCurrent = !showCurrent"
                                        class="absolute inset-y-0 right-0 flex items-center pr-4 text-gray-400 hover:text-gray-600">
                                    <i :class="showCurrent ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                                </button>
                            </div>
                            @error('current_password')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </div>

                        {{-- New Password --}}
                        <div>
                            <label for="password" class="block text-sm font-medium text-gray-700 mb-2">
                                New Password <span class="text-red-500">*</span>
                            </label>
                            <div class="relative">
                                <input :type="showNew ? 'text' : 'password'"
                                       id="password" name="password"
                                       required autocomplete="new-password"
                                       class="w-full px-4 py-3 pr-12 border-2 border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition @error('password') border-red-500 @enderror">
                                <button type="button" @click="showNew = !showNew"
                                        class="absolute inset-y-0 right-0 flex items-center pr-4 text-gray-400 hover:text-gray-600">
                                    <i :class="showNew ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                                </button>
                            </div>
                            @error('password')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </div>

                        {{-- Confirm Password --}}
                        <div>
                            <label for="password_confirmation" class="block text-sm font-medium text-gray-700 mb-2">
                                Confirm New Password <span class="text-red-500">*</span>
                            </label>
                            <div class="relative">
                                <input :type="showConfirm ? 'text' : 'password'"
                                       id="password_confirmation" name="password_confirmation"
                                       required autocomplete="new-password"
                                       class="w-full px-4 py-3 pr-12 border-2 border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent transition">
                                <button type="button" @click="showConfirm = !showConfirm"
                                        class="absolute inset-y-0 right-0 flex items-center pr-4 text-gray-400 hover:text-gray-600">
                                    <i :class="showConfirm ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                                </button>
                            </div>
                        </div>
                    </div>

                    <button type="submit" :disabled="loading"
                            class="px-6 py-3 bg-purple-600 text-white font-semibold rounded-lg hover:bg-purple-700 transition disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2">
                        <i x-show="!loading" class="fas fa-save"></i>
                        <i x-show="loading" class="fas fa-spinner fa-spin"></i>
                        <span x-text="loading ? 'Updating...' : 'Update Password'"></span>
                    </button>
                </form>
            </div>
        </div>

        {{-- Two-Factor Authentication --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
            <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-900 flex items-center gap-2">
                    <i class="fas fa-mobile-alt text-purple-600"></i> Two-Factor Authentication (2FA)
                </h3>
            </div>
            <div class="p-6">
                <div class="flex items-start justify-between gap-6">
                    <div class="flex-1">
                        <p class="text-sm text-gray-600 mb-3">
                            Two-factor authentication adds an extra layer of security by requiring a verification code in addition to your password.
                        </p>
                        @if(isset($twoFactorEnabled) && $twoFactorEnabled)
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-800">
                                <i class="fas fa-check-circle mr-2"></i>2FA is Enabled
                            </span>
                        @else
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-600">
                                <i class="fas fa-times-circle mr-2"></i>2FA is Disabled
                            </span>
                        @endif
                    </div>
                    <div class="flex-shrink-0">
                        @if(isset($twoFactorEnabled) && $twoFactorEnabled)
                            {{-- Disable 2FA — route is POST /two-factor/disable --}}
                            <form method="POST" action="{{ route('two-factor.disable') }}">
                                @csrf
                                <button type="submit"
                                        onclick="return confirm('Are you sure you want to disable 2FA?')"
                                        class="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition text-sm font-medium">
                                    <i class="fas fa-toggle-off mr-2"></i>Disable 2FA
                                </button>
                            </form>
                        @else
                            {{-- Enable 2FA — links to setup page (GET) --}}
                            <a href="{{ route('two-factor.setup') }}"
                               class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm font-medium inline-block">
                                <i class="fas fa-toggle-on mr-2"></i>Enable 2FA
                            </a>
                        @endif
                    </div>
                </div>
            </div>
        </div>

        {{-- Login History --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
            <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-900 flex items-center gap-2">
                    <i class="fas fa-history text-purple-600"></i> Recent Login History
                </h3>
            </div>
            <div class="p-0">
                @if(isset($loginAttempts) && $loginAttempts->count() > 0)
                    <div class="overflow-x-auto">
                        <table class="w-full table-zebra">
                            <thead>
                                <tr>
                                    <th class="px-4 py-3 text-left th-col-1">Date & Time</th>
                                    <th class="px-4 py-3 text-left th-col-2">IP Address</th>
                                    <th class="px-4 py-3 text-left th-col-3">Device</th>
                                    <th class="px-4 py-3 text-left th-col-4">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($loginAttempts as $attempt)
                                    <tr>
                                        <td class="px-4 py-3 text-sm text-gray-900">
                                            {{ $attempt->attempted_at->format('M d, Y H:i') }}
                                        </td>
                                        <td class="px-4 py-3 text-sm font-mono text-gray-600">
                                            {{ $attempt->ip_address ?? 'Unknown' }}
                                        </td>
                                        <td class="px-4 py-3 text-sm text-gray-600 max-w-xs truncate">
                                            {{ Str::limit($attempt->user_agent ?? 'Unknown', 50) }}
                                        </td>
                                        <td class="px-4 py-3">
                                            @if($attempt->successful)
                                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                                    <i class="fas fa-check mr-1"></i>Success
                                                </span>
                                            @else
                                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                                    <i class="fas fa-times mr-1"></i>Failed
                                                </span>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="text-center py-10">
                        <i class="fas fa-history text-4xl text-gray-300 mb-3"></i>
                        <p class="text-gray-500 text-sm">No login history available.</p>
                    </div>
                @endif
            </div>
        </div>

    </div>

</x-app-layout>
