<x-app-layout>
    <x-module-hero
        title="Google Meet Configuration"
        description="Manage OAuth2 credentials and API settings for Google Meet integration"
        icon="fa-video">
        <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="{{ $config ? route('config.google-meet.edit') : route('config.google-meet.create') }}"
               class="module-hero-tab {{ request()->routeIs('config.google-meet.create', '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-cog"></i> {{ $config ? '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-slot name="actions">
            @if(!$status['configured'])
                <a href="{{ route('config.google-meet.create') }}"
                   class="inline-flex items-center gap-1 px-3 py-1.5 text-xs font-medium text-white rounded bg-green-600 hover:bg-green-700 transition-colors">
                    <i class="fas fa-plus"></i> Configure Now
                </a>
            @else
                @if(!$status['authenticated'])
                    <a href="{{ route('config.google-meet.authenticate') }}"
                       class="inline-flex items-center gap-1 px-3 py-1.5 text-xs font-medium text-white rounded bg-blue-600 hover:bg-blue-700 transition-colors">
                        <i class="fab fa-google"></i> Authenticate with Google
                    </a>
                @endif
                <a href="{{ route('config.google-meet.test') }}"
                   class="inline-flex items-center gap-1 px-3 py-1.5 text-xs font-medium text-white rounded bg-purple-600 hover:bg-purple-700 transition-colors">
                    <i class="fas fa-plug"></i> Test Connection
                </a>
            @endif
        </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
        @if(session('info'))
            <div class="mb-4 p-4 bg-blue-100 border border-blue-400 text-blue-700 rounded-lg flex items-center">
                <i class="fas fa-info-circle mr-2"></i> {{ session('info') }}
            </div>
        @endif

        {{-- Stats Row --}}
        <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Configuration</p>
                <p class="text-xl font-bold mt-1 {{ $status['configured'] ? 'text-green-600' : 'text-red-500' }}">
                    {{ $status['configured'] ? 'Active' : 'Not Set' }}
                </p>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">OAuth Status</p>
                <p class="text-xl font-bold mt-1 {{ $status['authenticated'] ? 'text-green-600' : 'text-red-500' }}">
                    {{ $status['authenticated'] ? 'Connected' : 'Not Auth' }}
                </p>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Total Meetings</p>
                <p class="text-xl font-bold mt-1 text-blue-600">{{ $stats['total_meetings'] }}</p>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                <p class="text-xs text-gray-500 font-semibold uppercase tracking-wider">Connection Tests</p>
                <p class="text-xl font-bold mt-1 text-purple-600">{{ $stats['total_tests'] }}</p>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            {{-- Configuration Details --}}
            <div class="lg:col-span-2 space-y-6">
                {{-- Status Card --}}
                <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-cog text-green-600"></i> Integration Status
                        </h2>
                    </div>
                    <div class="p-6">
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
                            <div class="p-4 rounded-lg border {{ $status['configured'] ? 'border-green-200 bg-green-50' : 'border-yellow-200 bg-yellow-50' }}">
                                <p class="text-xs font-medium {{ $status['configured'] ? 'text-green-700' : 'text-yellow-700' }} mb-1">Configuration</p>
                                <p class="font-semibold {{ $status['configured'] ? 'text-green-800' : 'text-yellow-800' }}">
                                    <i class="fas {{ $status['configured'] ? 'fa-check-circle' : 'fa-exclamation-triangle' }} mr-1"></i>
                                    {{ $status['configured'] ? 'Configured' : 'Not Configured' }}
                                </p>
                            </div>
                            <div class="p-4 rounded-lg border {{ $status['authenticated'] ? 'border-green-200 bg-green-50' : 'border-red-200 bg-red-50' }}">
                                <p class="text-xs font-medium {{ $status['authenticated'] ? 'text-green-700' : 'text-red-700' }} mb-1">OAuth Authentication</p>
                                <p class="font-semibold {{ $status['authenticated'] ? 'text-green-800' : 'text-red-800' }}">
                                    <i class="fas {{ $status['authenticated'] ? 'fa-check-circle' : 'fa-times-circle' }} mr-1"></i>
                                    {{ $status['authenticated'] ? 'Connected' : 'Not Connected' }}
                                </p>
                            </div>
                            <div class="p-4 rounded-lg border {{ $status['has_valid_token'] ? 'border-green-200 bg-green-50' : 'border-gray-200 bg-gray-50' }}">
                                <p class="text-xs font-medium text-gray-600 mb-1">Token Status</p>
                                <p class="font-semibold text-gray-800">
                                    @if($status['has_valid_token'])
                                        <i class="fas fa-clock text-green-600 mr-1"></i>
                                        {{ $status['minutes_until_expiration'] }} min left
                                    @elseif($status['authenticated'])
                                        <i class="fas fa-sync-alt text-yellow-600 mr-1"></i> Needs Refresh
                                    @else
                                        <i class="fas fa-minus-circle text-gray-400 mr-1"></i> No Token
                                    @endif
                                </p>
                            </div>
                        </div>

                        @if($config)
                            <div class="space-y-3">
                                <div class="border border-gray-200 rounded-lg p-4">
                                    <h3 class="font-medium text-gray-800 mb-3 flex items-center gap-2">
                                        <i class="fas fa-key text-blue-500"></i> OAuth2 Credentials
                                    </h3>
                                    <dl class="grid grid-cols-1 md:grid-cols-2 gap-3">
                                        <div>
                                            <dt class="text-xs text-gray-500">Client ID</dt>
                                            <dd class="font-mono text-sm text-gray-800 truncate">
                                                {{ substr($config->client_id, 0, 20) }}...
                                            </dd>
                                        </div>
                                        <div>
                                            <dt class="text-xs text-gray-500">Redirect URI</dt>
                                            <dd class="font-mono text-sm text-gray-800 break-all">{{ $config->redirect_uri }}</dd>
                                        </div>
                                        <div>
                                            <dt class="text-xs text-gray-500">Created</dt>
                                            <dd class="text-sm text-gray-800">{{ $config->created_at->format('M d, Y H:i') }}</dd>
                                        </div>
                                        <div>
                                            <dt class="text-xs text-gray-500">Last Updated</dt>
                                            <dd class="text-sm text-gray-800">{{ $config->updated_at->format('M d, Y H:i') }}</dd>
                                        </div>
                                    </dl>
                                </div>

                                <div class="flex flex-wrap gap-2 pt-2">
                                    <a href="{{ route('config.google-meet.edit') }}"
                                       class="btn-action-edit">
                                        <i class="fas fa-edit"></i> Edit Configuration
                                    </a>
                                    @if(!$status['authenticated'])
                                        <a href="{{ route('config.google-meet.authenticate') }}"
                                           class="inline-flex items-center gap-1 px-3 py-1.5 text-xs font-medium text-white rounded bg-blue-600 hover:bg-blue-700 transition-colors">
                                            <i class="fab fa-google"></i> Authenticate with Google
                                        </a>
                                    @else
                                        <form method="POST" action="{{ route('config.google-meet.revoke') }}" class="inline"
                                              onsubmit="return confirm('Revoke Google access? You will need to re-authenticate.')">
                                            @csrf
                                            <button type="submit"
                                                    class="inline-flex items-center gap-1 px-3 py-1.5 text-xs font-medium text-white rounded bg-red-600 hover:bg-red-700 transition-colors">
                                                <i class="fas fa-unlink"></i> Revoke Access
                                            </button>
                                        </form>
                                    @endif
                                    <form method="POST" action="{{ route('config.google-meet.destroy') }}" class="inline"
                                          onsubmit="return confirm('Delete this configuration? This cannot be undone.')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit"
                                                class="btn-action-delete">
                                            <i class="fas fa-trash"></i> Delete
                                        </button>
                                    </form>
                                </div>
                            </div>
                        @else
                            <div class="text-center py-8">
                                <i class="fab fa-google text-gray-300 text-5xl mb-4"></i>
                                <h3 class="text-lg font-medium text-gray-900 mb-2">No Configuration Found</h3>
                                <p class="text-gray-500 mb-4 text-sm">Set up your Google OAuth2 credentials to enable Google Meet integration.</p>
                                <a href="{{ route('config.google-meet.create') }}"
                                   class="inline-flex items-center gap-1 px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm">
                                    <i class="fas fa-plus"></i> Configure Now
                                </a>
                            </div>
                        @endif
                    </div>
                </div>

                {{-- Recent Activity --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                    <div class="p-4 border-b border-gray-200 flex justify-between items-center">
                        <h2 class="text-base font-semibold text-gray-800 flex items-center gap-2">
                            <i class="fas fa-history text-gray-500"></i> Recent Activity
                        </h2>
                        <a href="{{ route('config.google-meet.logs') }}" class="text-xs text-green-600 hover:text-green-700">
                            View all <i class="fas fa-arrow-right ml-1"></i>
                        </a>
                    </div>
                    @if($recentLogs->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 text-white text-xs font-semibold uppercase tracking-wider">Type</th>
                                        <th class="px-4 py-3 text-left th-col-2 text-white text-xs font-semibold uppercase tracking-wider">Status</th>
                                        <th class="px-4 py-3 text-left th-col-3 text-white text-xs font-semibold uppercase tracking-wider">Summary</th>
                                        <th class="px-4 py-3 text-left th-col-4 text-white text-xs font-semibold uppercase tracking-wider">Time</th>
                                        <th class="px-4 py-3 text-left th-col-5 text-white text-xs font-semibold uppercase tracking-wider">Duration</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($recentLogs as $log)
                                        <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                                            <td class="px-4 py-3 text-sm">
                                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium
                                                    @if($log->type === 'create_meeting') bg-blue-100 text-blue-800
                                                    @elseif($log->type === 'test') bg-purple-100 text-purple-800
                                                    @elseif($log->type === 'token_refresh') bg-yellow-100 text-yellow-800
                                                    @else bg-gray-100 text-gray-800 @endif">
                                                    {{ $log->getTypeLabel() }}
                                                </span>
                                            </td>
                                            <td class="px-4 py-3 text-sm">
                                                @if($log->isSuccessful())
                                                    <span class="text-green-600"><i class="fas fa-check-circle mr-1"></i>Success</span>
                                                @else
                                                    <span class="text-red-600"><i class="fas fa-times-circle mr-1"></i>Failed</span>
                                                @endif
                                            </td>
                                            <td class="px-4 py-3 text-sm text-gray-700">{{ $log->summary ?? '-' }}</td>
                                            <td class="px-4 py-3 text-sm text-gray-500">{{ $log->created_at->diffForHumans() }}</td>
                                            <td class="px-4 py-3 text-sm text-gray-500">{{ $log->getFormattedExecutionTime() }}</td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    @else
                        <div class="p-8 text-center text-gray-500">
                            <i class="fas fa-inbox text-4xl mb-2 text-gray-300"></i>
                            <p class="text-sm">No activity logs yet. Configure and test your integration to see logs here.</p>
                        </div>
                    @endif
                </div>
            </div>

            {{-- Right Sidebar --}}
            <div class="space-y-4">
                {{-- Meeting Stats --}}
                <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-chart-bar text-indigo-500"></i> Meeting Statistics
                    </h3>
                    <dl class="space-y-2">
                        <div class="flex justify-between text-sm">
                            <dt class="text-gray-500">Total Meetings</dt>
                            <dd class="font-medium text-blue-600">{{ $stats['total_meetings'] }}</dd>
                        </div>
                        <div class="flex justify-between text-sm">
                            <dt class="text-gray-500">Successful</dt>
                            <dd class="font-medium text-green-600">{{ $stats['successful_meetings'] }}</dd>
                        </div>
                        <div class="flex justify-between text-sm">
                            <dt class="text-gray-500">Failed</dt>
                            <dd class="font-medium text-red-600">{{ $stats['failed_meetings'] }}</dd>
                        </div>
                        <div class="flex justify-between text-sm">
                            <dt class="text-gray-500">Connection Tests</dt>
                            <dd class="font-medium text-purple-600">{{ $stats['total_tests'] }}</dd>
                        </div>
                        <div class="flex justify-between text-sm">
                            <dt class="text-gray-500">Successful Tests</dt>
                            <dd class="font-medium text-green-600">{{ $stats['successful_tests'] }}</dd>
                        </div>
                        <div class="flex justify-between text-sm">
                            <dt class="text-gray-500">Token Refreshes</dt>
                            <dd class="font-medium text-yellow-600">{{ $stats['token_refreshes'] }}</dd>
                        </div>
                    </dl>
                </div>

                {{-- Quick Actions --}}
                <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-bolt text-yellow-500"></i> Quick Actions
                    </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-blue-500 w-4"></i> View Documentation
                        </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="fab fa-google text-green-500 w-4"></i> Google Cloud Console
                        </a>
                        <a href="{{ route('config.google-meet.logs') }}"
                           class="flex items-center gap-2 p-2 rounded-lg hover:bg-gray-50 transition text-sm text-gray-700">
                            <i class="fas fa-chart-line text-purple-500 w-4"></i> View All Logs
                        </a>
                    </div>
                </div>

                {{-- Last Meeting --}}
                @if($stats['last_meeting'])
                    <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-video text-green-500"></i> Last Meeting
                        </h3>
                        <p class="text-sm text-gray-700 font-medium">{{ $stats['last_meeting']->summary ?? 'Untitled' }}</p>
                        <p class="text-xs text-gray-500 mt-1">{{ $stats['last_meeting']->created_at->diffForHumans() }}</p>
                        @if($stats['last_meeting']->meeting_link)
                            <a href="{{ $stats['last_meeting']->meeting_link }}" target="_blank"
                               class="text-xs text-blue-600 hover:underline mt-1 block">
                                <i class="fas fa-external-link-alt mr-1"></i> Open Meeting
                            </a>
                        @endif
                    </div>
                @endif
            </div>
        </div>
    </div>
</x-app-layout>
