{{-- Onglet IA & Insights --}}
<div class="row g-4">
    {{-- Statut IA --}}
    <div class="col-12">
        <div class="alert {{ $aiEnabled ?? false ? 'alert-success' : 'alert-warning' }} d-flex align-items-center">
            <i class="bi bi-robot me-3 fs-3"></i>
            <div>
                @if($aiEnabled ?? false)
                    <strong>Module IA Activé</strong>
                    <p class="mb-0 small">L'IA analyse vos données et génère des insights automatiquement.</p>
                @else
                    <strong>Module IA Désactivé</strong>
                    <p class="mb-0 small">Activez le module IA dans <a href="{{ route('admin.ai.index') }}">les paramètres IA</a> pour bénéficier des analyses automatiques.</p>
                @endif
            </div>
        </div>
    </div>
    
    {{-- Alertes Importantes --}}
    @if(isset($alerts) && count($alerts) > 0)
    <div class="col-12">
        <div class="card border-danger">
            <div class="card-header bg-danger text-white">
                <h5 class="mb-0"><i class="bi bi-exclamation-triangle me-2"></i>Alertes à Traiter ({{ count($alerts) }})</h5>
            </div>
            <div class="card-body">
                @foreach($alerts as $alert)
                <div class="alert alert-ai alert-{{ $alert['type'] }} mb-3">
                    <div class="d-flex justify-content-between align-items-start">
                        <div>
                            <strong>{{ $alert['title'] }}</strong>
                            <p class="mb-1">{{ $alert['message'] }}</p>
                            <small class="text-muted"><i class="bi bi-lightning-charge"></i> Action suggérée: {{ $alert['action'] }}</small>
                        </div>
                        <span class="badge bg-{{ $alert['type'] }}">Urgent</span>
                    </div>
                </div>
                @endforeach
            </div>
        </div>
    </div>
    @endif
    
    {{-- Insights --}}
    @if(isset($insights) && count($insights) > 0)
    <div class="col-md-6">
        <div class="card h-100">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-lightbulb text-info me-2"></i>Insights Automatiques</h5>
            </div>
            <div class="card-body">
                @foreach($insights as $insight)
                <div class="alert alert-ai alert-{{ $insight['type'] }} mb-3">
                    <strong>{{ $insight['title'] }}</strong>
                    <p class="mb-0 small">{{ $insight['message'] }}</p>
                </div>
                @endforeach
            </div>
        </div>
    </div>
    @endif
    
    {{-- Recommandations --}}
    @if(isset($recommendations) && count($recommendations) > 0)
    <div class="col-md-6">
        <div class="card h-100">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-stars text-warning me-2"></i>Recommandations IA</h5>
            </div>
            <div class="card-body">
                @foreach($recommendations as $rec)
                <div class="d-flex mb-3 pb-3 border-bottom">
                    <span class="priority-badge priority-{{ $rec['priority'] }} me-3">
                        {{ $rec['priority'] === 'high' ? 'Haute' : ($rec['priority'] === 'medium' ? 'Moyenne' : 'Basse') }}
                    </span>
                    <div>
                        <strong>{{ $rec['title'] }}</strong>
                        <p class="mb-0 small text-muted">{{ $rec['description'] }}</p>
                    </div>
                </div>
                @endforeach
            </div>
        </div>
    </div>
    @endif
    
    {{-- Membres à Contacter en Priorité --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-telephone text-primary me-2"></i>Membres à Contacter en Priorité</h5>
                <small class="text-muted">Basé sur l'analyse IA des achats AMM</small>
            </div>
            <div class="card-body p-0">
                @if(isset($membersToContact) && $membersToContact->isNotEmpty())
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Priorité</th>
                                    <th>Membre</th>
                                    <th>Contact</th>
                                    <th class="text-center">Progression AMM</th>
                                    <th class="text-end">Restant</th>
                                    <th class="text-center">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($membersToContact as $member)
                                <tr class="member-row">
                                    <td>
                                        <span class="priority-badge priority-{{ $member->priority }}">
                                            {{ $member->priority === 'high' ? '🔥 Haute' : ($member->priority === 'medium' ? '⚠️ Moyenne' : '📋 Basse') }}
                                        </span>
                                    </td>
                                    <td><strong>{{ $member->name }}</strong></td>
                                    <td>
                                        <small>
                                            @if($member->phone)
                                                <i class="bi bi-phone"></i> {{ $member->phone }}<br>
                                            @endif
                                            @if($member->email)
                                                <i class="bi bi-envelope"></i> {{ Str::limit($member->email, 20) }}
                                            @endif
                                        </small>
                                    </td>
                                    <td style="min-width: 150px;">
                                        <div class="progress progress-slim mb-1">
                                            <div class="progress-bar bg-{{ $member->percentage >= 75 ? 'success' : ($member->percentage >= 50 ? 'warning' : 'danger') }}" 
                                                 style="width: {{ $member->percentage }}%"></div>
                                        </div>
                                        <div class="d-flex justify-content-between">
                                            <small>{{ number_format($member->monthly_purchase, 0, ',', ' ') }} XOF</small>
                                            <small class="fw-bold">{{ $member->percentage }}%</small>
                                        </div>
                                    </td>
                                    <td class="text-end">
                                        <span class="text-danger fw-bold">{{ number_format($member->remaining, 0, ',', ' ') }} XOF</span>
                                    </td>
                                    <td class="text-center">
                                        @if($member->email)
                                            <form action="{{ route('admin.analytics.send-amm-reminder') }}" method="POST" class="d-inline">
                                                @csrf
                                                <input type="hidden" name="member_id" value="{{ $member->id }}">
                                                <input type="hidden" name="channel" value="email">
                                                <button type="submit" class="btn btn-sm btn-outline-primary" title="Envoyer rappel email">
                                                    <i class="bi bi-envelope"></i>
                                                </button>
                                            </form>
                                        @endif
                                        @if($member->whatsapp)
                                            <form action="{{ route('admin.analytics.send-amm-reminder') }}" method="POST" class="d-inline">
                                                @csrf
                                                <input type="hidden" name="member_id" value="{{ $member->id }}">
                                                <input type="hidden" name="channel" value="whatsapp">
                                                <button type="submit" class="btn btn-sm btn-outline-success" title="Contacter via WhatsApp">
                                                    <i class="bi bi-whatsapp"></i>
                                                </button>
                                            </form>
                                        @endif
                                        @if($member->phone)
                                            <a href="tel:{{ $member->phone }}" class="btn btn-sm btn-outline-info" title="Appeler">
                                                <i class="bi bi-telephone"></i>
                                            </a>
                                        @endif
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="text-center text-muted py-5">
                        <i class="bi bi-emoji-smile text-success" style="font-size: 3rem;"></i>
                        <p class="mt-2">Tous les membres ont atteint leur AMM ce mois !</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
    
    {{-- Actions Rapides IA --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="bi bi-gear text-secondary me-2"></i>Actions Rapides</h5>
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-3">
                        <a href="{{ route('admin.ai.stagnant-leaders') }}" class="btn btn-outline-warning w-100 py-3">
                            <i class="bi bi-person-exclamation fs-4 d-block mb-2"></i>
                            Détecter Leaders Stagnants
                        </a>
                    </div>
                    <div class="col-md-3">
                        <form action="{{ route('admin.ai.generate-report') }}" method="POST" class="d-inline w-100">
                            @csrf
                            <input type="hidden" name="type" value="sales">
                            <input type="hidden" name="period" value="monthly">
                            <button type="submit" class="btn btn-outline-primary w-100 py-3">
                                <i class="bi bi-file-earmark-bar-graph fs-4 d-block mb-2"></i>
                                Générer Rapport Ventes
                            </button>
                        </form>
                    </div>
                    <div class="col-md-3">
                        <form action="{{ route('admin.ai.generate-report') }}" method="POST" class="d-inline w-100">
                            @csrf
                            <input type="hidden" name="type" value="mlm">
                            <input type="hidden" name="period" value="monthly">
                            <button type="submit" class="btn btn-outline-success w-100 py-3">
                                <i class="bi bi-diagram-3 fs-4 d-block mb-2"></i>
                                Générer Rapport MLM
                            </button>
                        </form>
                    </div>
                    <div class="col-md-3">
                        <form action="{{ route('admin.analytics.bulk-amm-reminders') }}" method="POST" class="d-inline w-100">
                            @csrf
                            <button type="submit" class="btn btn-outline-danger w-100 py-3" onclick="return confirm('Envoyer des rappels AMM à tous les membres concernés ?')">
                                <i class="bi bi-send-exclamation fs-4 d-block mb-2"></i>
                                Rappels AMM en Masse
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
