<x-app-layout>
    <x-module-hero
        title="{{ $isEdit ? 'Edit Google Meet Configuration' : 'New Google Meet Configuration' }}"
        description="{{ $isEdit ? 'Update your Google OAuth2 credentials' : 'Configure Google OAuth2 credentials for Meet API access' }}"
        icon="fa-video">
        <x-slot name="backButton">
            <a href="{{ route('config.google-meet.index') }}"
               class="inline-flex items-center gap-1 text-white/80 hover:text-white text-sm transition-colors">
                <i class="fas fa-arrow-left"></i> Back to Dashboard
            </a>
        </x-slot>
        <x-slot name="tabs">
            <a href="{{ route('config.google-meet.index') }}"
               class="module-hero-tab {{ request()->routeIs('config.google-meet.index') ? '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 transition">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            <a href="{{ $isEdit ? route('config.google-meet.edit') : route('config.google-meet.create') }}"
               class="module-hero-tab {{ request()->routeIs('config.google-meet.create') || request()->routeIs('config.google-meet.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 transition">
                <i class="fas fa-{{ $isEdit ? 'edit' : 'plus' }}"></i>
                {{ $isEdit ? 'Edit Configuration' : 'New Configuration' }}
            </a>
            <a href="{{ route('config.google-meet.test') }}"
               class="module-hero-tab {{ request()->routeIs('config.google-meet.test') ? '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 transition">
                <i class="fas fa-plug"></i> Test Connection
            </a>
            <a href="{{ route('config.google-meet.logs') }}"
               class="module-hero-tab {{ request()->routeIs('config.google-meet.logs') ? '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 transition">
                <i class="fas fa-history"></i> Activity Logs
            </a>
            <a href="{{ route('config.google-meet.documentation') }}"
               class="module-hero-tab {{ request()->routeIs('config.google-meet.documentation') ? '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 transition">
                <i class="fas fa-book"></i> Documentation
            </a>
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        {{-- Flash Messages --}}
        @if(session('success'))
            <div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center">
                <i class="fas fa-check-circle mr-2"></i> {{ session('success') }}
            </div>
        @endif
        @if(session('error'))
            <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center">
                <i class="fas fa-exclamation-circle mr-2"></i> {{ session('error') }}
            </div>
        @endif

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            {{-- Main Form --}}
            <div class="lg:col-span-2">
                {{-- Setup Instructions --}}
                <div class="mb-6 bg-blue-50 border border-blue-200 rounded-lg p-5">
                    <h3 class="text-sm font-semibold text-blue-800 mb-3 flex items-center gap-2">
                        <i class="fas fa-info-circle"></i> Setup Instructions
                    </h3>
                    <ol class="list-decimal list-inside space-y-1.5 text-blue-700 text-sm">
                        <li>Go to <a href="https://console.cloud.google.com/apis/credentials" target="_blank" class="underline font-medium">Google Cloud Console</a></li>
                        <li>Create OAuth 2.0 Client ID credentials (Web application type)</li>
                        <li>Add the Redirect URI below to "Authorized redirect URIs"</li>
                        <li>Enable Google Calendar API in your project</li>
                        <li>Copy the Client ID and Client Secret to the form below</li>
                    </ol>
                    <div class="mt-3 p-3 bg-white rounded border border-blue-200">
                        <p class="text-xs text-blue-600 font-medium mb-1">Your Redirect URI (add this to Google Cloud Console):</p>
                        <code class="text-xs bg-blue-50 p-2 rounded block overflow-x-auto text-blue-800">
                            {{ config('app.url') }}/config/google-meet/callback
                        </code>
                    </div>
                </div>

                {{-- Configuration Form --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                    <div class="p-4 border-b border-gray-200">
                        <h2 class="text-base font-semibold text-gray-800 flex items-center gap-2">
                            <i class="fas fa-key text-green-600"></i> OAuth2 Credentials
                        </h2>
                    </div>

                    <form method="POST"
                          action="{{ $isEdit ? route('config.google-meet.update') : route('config.google-meet.store') }}"
                          class="p-6">
                        @csrf
                        @if($isEdit)
                            @method('PUT')
                        @endif

                        <div class="grid grid-cols-1 gap-6">
                            {{-- Client ID --}}
                            <div>
                                <label for="client_id" class="block text-sm font-medium text-gray-700 mb-1">
                                    Google Client ID <span class="text-red-500">*</span>
                                </label>
                                <div class="relative">
                                    <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                        <i class="fas fa-id-card text-gray-400"></i>
                                    </div>
                                    <input type="text"
                                           id="client_id"
                                           name="client_id"
                                           value="{{ old('client_id', $config?->client_id ?? '') }}"
                                           placeholder="123456789-abcdefg.apps.googleusercontent.com"
                                           class="w-full pl-10 pr-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm"
                                           required
                                           autocomplete="off">
                                </div>
                                @error('client_id')
                                    <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                                @enderror
                                <p class="mt-1 text-xs text-gray-500">Found in Google Cloud Console → APIs & Services → Credentials</p>
                            </div>

                            {{-- Client Secret --}}
                            <div>
                                <label for="client_secret" class="block text-sm font-medium text-gray-700 mb-1">
                                    Google Client Secret <span class="text-red-500">*</span>
                                </label>
                                <div class="relative">
                                    <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                        <i class="fas fa-lock text-gray-400"></i>
                                    </div>
                                    <input type="password"
                                           id="client_secret"
                                           name="client_secret"
                                           value="{{ old('client_secret', $config?->client_secret ?? '') }}"
                                           placeholder="GOCSPX-xxxxxxxxxxxxxxxxx"
                                           class="w-full pl-10 pr-12 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm"
                                           required
                                           autocomplete="new-password">
                                    <button type="button"
                                            onclick="togglePasswordVisibility('client_secret')"
                                            class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 hover:text-gray-600">
                                        <i class="fas fa-eye" id="client_secret_icon"></i>
                                    </button>
                                </div>
                                @error('client_secret')
                                    <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                                @enderror
                                <p class="mt-1 text-xs text-gray-500">Secret key from your OAuth 2.0 Client ID credentials</p>
                            </div>

                            {{-- Redirect URI --}}
                            <div>
                                <label for="redirect_uri" class="block text-sm font-medium text-gray-700 mb-1">
                                    Redirect URI <span class="text-red-500">*</span>
                                </label>
                                <div class="relative">
                                    <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                        <i class="fas fa-link text-gray-400"></i>
                                    </div>
                                    <input type="url"
                                           id="redirect_uri"
                                           name="redirect_uri"
                                           value="{{ old('redirect_uri', $config?->redirect_uri ?? config('app.url') . '/config/google-meet/callback') }}"
                                           class="w-full pl-10 pr-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm"
                                           required>
                                </div>
                                @error('redirect_uri')
                                    <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                                @enderror
                                <p class="mt-1 text-xs text-gray-500">Must match the URI configured in Google Cloud Console exactly</p>
                            </div>

                            {{-- Security Notice --}}
                            <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4">
                                <div class="flex items-start gap-3">
                                    <i class="fas fa-shield-alt text-yellow-600 mt-0.5"></i>
                                    <div>
                                        <h4 class="text-sm font-medium text-yellow-800">Security Notice</h4>
                                        <p class="text-xs text-yellow-700 mt-1">
                                            All credentials are encrypted at rest using Laravel's encryption. Never share your Client Secret with anyone.
                                        </p>
                                    </div>
                                </div>
                            </div>

                            {{-- Action Buttons --}}
                            <div class="flex items-center justify-between pt-4 border-t border-gray-200">
                                <a href="{{ route('config.google-meet.index') }}"
                                   class="inline-flex items-center gap-1 px-4 py-2 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 transition text-sm">
                                    <i class="fas fa-arrow-left"></i> Cancel
                                </a>
                                <div class="flex gap-2">
                                    @if($isEdit && $config)
                                        <a href="https://console.cloud.google.com/apis/credentials" target="_blank"
                                           class="inline-flex items-center gap-1 px-4 py-2 border border-blue-300 text-blue-600 rounded-lg hover:bg-blue-50 transition text-sm">
                                            <i class="fab fa-google"></i> Open Console
                                        </a>
                                    @endif
                                    <button type="submit"
                                            class="inline-flex items-center gap-1 px-5 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm font-medium">
                                        <i class="fas fa-save"></i>
                                        {{ $isEdit ? 'Update Configuration' : 'Save Configuration' }}
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            {{-- Right Sidebar --}}
            <div class="space-y-4">
                {{-- Help --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <h3 class="text-sm font-semibold text-gray-800 mb-3 flex items-center gap-2">
                        <i class="fas fa-question-circle text-blue-500"></i> Need Help?
                    </h3>
                    <div class="space-y-2">
                        <a href="{{ route('config.google-meet.documentation') }}"
                           class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-50 transition text-sm text-gray-700">
                            <i class="fas fa-book text-green-500 w-4"></i> Integration Guide
                        </a>
                        <a href="https://developers.google.com/calendar/api/guides/overview" target="_blank"
                           class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-50 transition text-sm text-gray-700">
                            <i class="fab fa-google text-blue-500 w-4"></i> Google API Docs
                        </a>
                        <a href="https://console.cloud.google.com/apis/credentials" target="_blank"
                           class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-50 transition text-sm text-gray-700">
                            <i class="fas fa-external-link-alt text-gray-400 w-4"></i> Google Cloud Console
                        </a>
                    </div>
                </div>

                {{-- Required Scopes --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <h3 class="text-sm font-semibold text-gray-800 mb-3 flex items-center gap-2">
                        <i class="fas fa-list-check text-purple-500"></i> Required OAuth Scopes
                    </h3>
                    <div class="bg-gray-50 rounded p-3 space-y-1">
                        <code class="text-xs text-gray-700 block">https://www.googleapis.com/auth/calendar.events</code>
                        <code class="text-xs text-gray-700 block">https://www.googleapis.com/auth/calendar</code>
                    </div>
                    <p class="text-xs text-gray-500 mt-2">Add these scopes to your OAuth consent screen in Google Cloud Console.</p>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        function togglePasswordVisibility(fieldId) {
            const field = document.getElementById(fieldId);
            const icon = document.getElementById(fieldId + '_icon');
            if (field.type === 'password') {
                field.type = 'text';
                icon.classList.replace('fa-eye', 'fa-eye-slash');
            } else {
                field.type = 'password';
                icon.classList.replace('fa-eye-slash', 'fa-eye');
            }
        }
    </script>
    @endpush
</x-app-layout>
