@extends('layouts.admin')

@section('title', 'Test Configuration AI')

@section('content')
<style>
    .test-card { background: white; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); overflow: hidden; margin-bottom: 20px; }
    .test-header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; }
    .test-item { padding: 24px; border-bottom: 1px solid #e5e7eb; }
    .test-item:last-child { border-bottom: none; }
    .status-badge { padding: 8px 16px; border-radius: 20px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
    .status-ok { background: #ecfdf5; color: #059669; }
    .status-error { background: #fef2f2; color: #dc2626; }
    .status-warning { background: #fef3c7; color: #d97706; }
    .code-block { background: #f8fafc; padding: 12px; border-radius: 8px; border: 1px solid #e2e8f0; font-family: 'Courier New', monospace; font-size: 0.9em; }
</style>

<div class="container-fluid py-4">
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h1 class="h3 mb-1"><i class="bi bi-robot me-2"></i>Test Configuration AI</h1>
                    <p class="text-muted mb-0">Diagnostic de l'intégration OpenAI</p>
                </div>
                <a href="{{ route('admin.features.index') }}" class="btn btn-secondary">
                    <i class="bi bi-arrow-left me-2"></i>Retour
                </a>
            </div>
        </div>
    </div>

    <!-- Test 1: Environment -->
    <div class="test-card">
        <div class="test-item">
            <div class="d-flex justify-content-between align-items-start">
                <div>
                    <h5 class="mb-2"><i class="bi bi-check-circle-fill text-success me-2"></i>Environnement Laravel</h5>
                    <p class="text-muted mb-2">L'application Laravel est correctement chargée</p>
                    <small class="text-muted">Environment: <code>{{ app()->environment() }}</code></small>
                </div>
                <span class="status-badge status-ok">
                    <i class="bi bi-check-circle"></i> OK
                </span>
            </div>
        </div>
    </div>

    <!-- Test 2: .env Configuration -->
    <div class="test-card">
        <div class="test-item">
            <div class="d-flex justify-content-between align-items-start">
                <div class="flex-grow-1">
                    <h5 class="mb-2"><i class="bi bi-file-earmark-code me-2"></i>Configuration .env</h5>
                    @if($openaiKey && $openaiKey !== 'your_openai_api_key')
                        <p class="text-muted mb-2">Clé API OpenAI configurée</p>
                        <div class="code-block">
                            OPENAI_API_KEY={{ substr($openaiKey, 0, 20) }}...{{ substr($openaiKey, -10) }}
                        </div>
                    @else
                        <p class="text-danger mb-2">La clé OpenAI n'est pas configurée dans .env</p>
                        <div class="alert alert-warning mt-2">
                            <small>Ajoutez <code>OPENAI_API_KEY=votre_clé</code> dans le fichier .env</small>
                        </div>
                    @endif
                </div>
                <span class="status-badge {{ $openaiKey && $openaiKey !== 'your_openai_api_key' ? 'status-ok' : 'status-error' }}">
                    @if($openaiKey && $openaiKey !== 'your_openai_api_key')
                        <i class="bi bi-check-circle"></i> OK
                    @else
                        <i class="bi bi-x-circle"></i> ERREUR
                    @endif
                </span>
            </div>
        </div>
    </div>

    <!-- Test 3: Config File -->
    <div class="test-card">
        <div class="test-item">
            <div class="d-flex justify-content-between align-items-start">
                <div class="flex-grow-1">
                    <h5 class="mb-2"><i class="bi bi-gear me-2"></i>Fichier config/ai.php</h5>
                    @if($configExists)
                        <p class="text-muted mb-2">Le fichier de configuration AI existe</p>
                        <ul class="mt-2 mb-0">
                            <li>Model: <code>{{ config('ai.openai.default_model', 'N/A') }}</code></li>
                            <li>Temperature: <code>{{ config('ai.openai.temperature', 'N/A') }}</code></li>
                            <li>Max Tokens: <code>{{ config('ai.openai.max_tokens', 'N/A') }}</code></li>
                        </ul>
                    @else
                        <p class="text-danger mb-2">Le fichier config/ai.php n'existe pas</p>
                    @endif
                </div>
                <span class="status-badge {{ $configExists ? 'status-ok' : 'status-error' }}">
                    @if($configExists)
                        <i class="bi bi-check-circle"></i> OK
                    @else
                        <i class="bi bi-x-circle"></i> ERREUR
                    @endif
                </span>
            </div>
        </div>
    </div>

    <!-- Test 4: Database Features -->
    <div class="test-card">
        <div class="test-item">
            <div class="d-flex justify-content-between align-items-start">
                <div class="flex-grow-1">
                    <h5 class="mb-2"><i class="bi bi-database me-2"></i>Fonctionnalités AI en base</h5>
                    @if($aiFeatures->count() > 0)
                        <p class="text-muted mb-3">{{ $aiFeatures->count() }} fonctionnalités AI trouvées</p>
                        <ul class="mb-0">
                            @foreach($aiFeatures as $feature)
                                <li class="mb-2">
                                    <div class="d-flex align-items-center gap-2">
                                        @if($feature->is_enabled)
                                            <i class="bi bi-toggle-on text-success"></i>
                                        @else
                                            <i class="bi bi-toggle-off text-secondary"></i>
                                        @endif
                                        <strong>{{ $feature->name }}</strong>
                                        <code class="text-muted">{{ $feature->feature_key }}</code>
                                        <span class="badge {{ $feature->is_enabled ? 'bg-success' : 'bg-secondary' }} ms-2">
                                            {{ $feature->is_enabled ? 'Activé' : 'Désactivé' }}
                                        </span>
                                    </div>
                                    @if(isset($feature->config['api_key']) && $feature->config['api_key'])
                                        <small class="text-muted ms-4 d-block">
                                            API Key: {{ substr($feature->config['api_key'], 0, 15) }}...{{ substr($feature->config['api_key'], -8) }}
                                        </small>
                                    @endif
                                </li>
                            @endforeach
                        </ul>
                    @else
                        <p class="text-warning mb-2">Aucune fonctionnalité AI trouvée</p>
                        <a href="{{ route('admin.features.initialize') }}" class="btn btn-sm btn-primary">
                            <i class="bi bi-database-add me-1"></i>Initialiser les fonctionnalités
                        </a>
                    @endif
                </div>
                <span class="status-badge {{ $aiFeatures->count() > 0 ? 'status-ok' : 'status-warning' }}">
                    @if($aiFeatures->count() > 0)
                        <i class="bi bi-check-circle"></i> {{ $aiFeatures->count() }} features
                    @else
                        <i class="bi bi-exclamation-triangle"></i> AUCUNE
                    @endif
                </span>
            </div>
        </div>
    </div>

    <!-- Test 5: API Connection -->
    <div class="test-card">
        <div class="test-item">
            <div class="d-flex justify-content-between align-items-start">
                <div class="flex-grow-1">
                    <h5 class="mb-2"><i class="bi bi-cloud-check me-2"></i>Test de connexion OpenAI</h5>
                    @if($apiTest['status'] === 'success')
                        <p class="text-success mb-2"><i class="bi bi-check-circle-fill me-1"></i>La clé API est valide et fonctionnelle!</p>
                        <p class="text-muted mb-0"><strong>{{ $apiTest['models_count'] }} modèles</strong> disponibles</p>
                    @elseif($apiTest['status'] === 'error')
                        <p class="text-danger mb-2"><i class="bi bi-x-circle-fill me-1"></i>Erreur de connexion</p>
                        <div class="alert alert-danger">
                            <small>{{ $apiTest['message'] }}</small>
                        </div>
                    @else
                        <p class="text-warning mb-0"><i class="bi bi-exclamation-triangle-fill me-1"></i>Clé API non configurée</p>
                    @endif
                </div>
                <span class="status-badge status-{{ $apiTest['status'] === 'success' ? 'ok' : ($apiTest['status'] === 'error' ? 'error' : 'warning') }}">
                    @if($apiTest['status'] === 'success')
                        <i class="bi bi-check-circle"></i> CONNEXION OK
                    @elseif($apiTest['status'] === 'error')
                        <i class="bi bi-x-circle"></i> ERREUR
                    @else
                        <i class="bi bi-exclamation-triangle"></i> SKIP
                    @endif
                </span>
            </div>
        </div>
    </div>

    <!-- Summary -->
    <div class="test-card">
        <div class="test-item bg-light">
            <h5 class="mb-3"><i class="bi bi-clipboard-check me-2"></i>Résumé</h5>
            @php
                $allGood = $openaiKey && 
                          $openaiKey !== 'your_openai_api_key' && 
                          $configExists && 
                          $aiFeatures->count() > 0 &&
                          $apiTest['status'] === 'success';
            @endphp
            
            @if($allGood)
                <div class="alert alert-success mb-0">
                    <i class="bi bi-check-circle-fill me-2"></i>
                    <strong>Tout est prêt!</strong> Les fonctionnalités AI peuvent être utilisées.
                </div>
            @else
                <div class="alert alert-warning mb-0">
                    <i class="bi bi-exclamation-triangle-fill me-2"></i>
                    <strong>Configuration incomplète.</strong> Certains éléments doivent être configurés.
                </div>
            @endif
            
            <div class="mt-3 d-flex gap-2">
                <a href="{{ route('admin.features.config-all') }}" class="btn btn-primary">
                    <i class="bi bi-gear me-2"></i>Configuration avancée
                </a>
                <a href="{{ route('admin.features.ai-config') }}" class="btn btn-outline-primary">
                    <i class="bi bi-robot me-2"></i>Config AI
                </a>
            </div>
        </div>
    </div>
</div>
@endsection
