@extends('layouts.app')

@section('title', 'Dashboard Admin')

@section('content')
    <div class="bg-gray-100 min-h-screen pb-10">
        <div class="container max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">

            <div
                class="mb-8 bg-gradient-to-r from-blue-700 to-blue-500 rounded-2xl p-6 shadow-lg flex flex-col md:flex-row justify-between items-start md:items-center gap-2">
                <div>
                    <h1 class="text-2xl md:text-3xl font-bold text-white">Dashboard Admin</h1>
                    <p class="text-blue-100 text-sm mt-1">
                        Selamat datang, <span class="font-semibold">{{ Auth::user()->name }}</span>
                    </p>
                </div>
                <div class="text-white text-sm">
                    {{ now()->format('l, d M Y') }}
                </div>
            </div>

            <h3 class="font-semibold text-gray-700 mb-3">📊 Ringkasan Sistem</h3>

            <div class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-7 gap-4 mb-10">
                @foreach ([
            ['label' => 'Pengguna', 'value' => $userCount, 'color' => 'sky'],
            ['label' => 'Produk', 'value' => $productCount, 'color' => 'amber'], // 🔥 ganti dari orange → amber (lebih soft)
            ['label' => 'Artikel', 'value' => $articleCount, 'color' => 'green'],
            ['label' => 'Kategori', 'value' => $categoryCount, 'color' => 'purple'],
            ['label' => 'Total Pertanyaan', 'value' => $totalChats, 'color' => 'indigo'],
            ['label' => 'Accuracy (Evaluasi Admin)', 'value' => round($accuracy) . '%', 'color' => 'cyan'],
            ['label' => 'Jawaban Tidak Sesuai', 'value' => $knowledgeGap, 'color' => 'rose'],
        ] as $stat)
                    <div class="bg-white rounded-xl shadow hover:shadow-md transition p-4 border text-center">
                        <p class="text-xs text-gray-500">{{ $stat['label'] }}</p>
                        <p class="text-2xl font-bold text-{{ $stat['color'] }}-600 mt-1">
                            {{ $stat['value'] ?? 0 }}
                        </p>
                    </div>
                @endforeach
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 items-stretch">
                <div>
                    <div
                        class="bg-white rounded-xl shadow p-5 border h-full flex flex-col justify-between lg:sticky lg:top-7">

                        <h3 class="font-semibold mb-4 text-gray-700 flex items-center gap-2">
                            ⚡ <span>Akses Cepat</span>
                        </h3>

                        <div class="space-y-2 text-sm font-semibold">

                            <a href="{{ route('admin.articles.index') }}"
                                class="flex items-center gap-2 p-3 rounded-lg bg-blue-50 text-blue-700 hover:bg-blue-100">
                                📝 Kelola Artikel
                            </a>

                            <a href="{{ route('admin.products.index') }}"
                                class="flex items-center gap-2 p-3 rounded-lg bg-orange-50 text-orange-700 hover:bg-orange-100">
                                📦 Kelola Produk
                            </a>

                            <a href="{{ route('admin.categories.index') }}"
                                class="flex items-center gap-2 p-3 rounded-lg bg-purple-50 text-purple-700 hover:bg-purple-100">
                                📁 Kelola Kategori
                            </a>

                            <a href="{{ route('admin.users.index') }}"
                                class="flex items-center gap-2 p-3 rounded-lg bg-green-50 text-green-700 hover:bg-green-100">
                                👤 Kelola Admin
                            </a>

                            <a href="{{ route('admin.documents.index') }}"
                                class="flex items-center gap-2 p-3 rounded-lg bg-yellow-50 text-yellow-700 hover:bg-yellow-100">
                                🤖 Pusat Data Chatbot
                            </a>

                            <a href="{{ route('admin.chatlogs.index') }}"
                                class="flex items-center gap-2 p-3 rounded-lg bg-indigo-50 text-indigo-700 hover:bg-indigo-100">
                                📊 Log Percakapan AI
                            </a>

                            <a href="{{ route('admin.pesan.index') }}"
                                class="flex items-center justify-between p-3 rounded-lg bg-teal-50 text-teal-700 hover:bg-teal-100">
                                <span>💬 Pesan</span>
                                <span class="text-[10px] bg-teal-500 text-white px-1.5 py-0.5 rounded-full">
                                    {{ $unreadCount ?? 0 }}
                                </span>
                            </a>

                        </div>
                    </div>
                </div>

                <!-- 📊 ANALYTICS -->
                <div class="lg:col-span-2">
                    <div class="bg-white rounded-xl shadow p-6 border h-full flex flex-col min-h-[420px]">

                        <h3 class="font-semibold mb-5 text-gray-700">📈 Analisis Ketersediaan Pengetahuan (RAG)</h3>

                        <div class="flex flex-col lg:flex-row gap-6 items-center flex-1">

                            <!-- CHART -->
                            <div class="relative w-52 h-52 mx-auto">
                                <canvas id="aiPerformanceChart"></canvas>

                                <div class="absolute inset-0 flex flex-col items-center justify-center">
                                    <span class="text-3xl font-extrabold text-gray-800">
                                        {{ $validContext + $knowledgeGap > 0 ? round(($validContext / ($validContext + $knowledgeGap)) * 100) : 0 }}%
                                    </span>
                                    <span class="text-xs text-gray-500">Success Rate</span>
                                </div>
                            </div>
                            <div class="w-full lg:w-1/2 space-y-4">

                                <div class="bg-slate-50 p-4 rounded-xl border">

                                    <h5 class="font-bold text-sm text-gray-700 mb-3">Metrik RAG</h5>

                                    <div class="space-y-2 text-sm">

                                        <div class="flex justify-between">
                                            <span>Jawaban Benar</span>
                                            <span class="text-green-600 font-bold">{{ $validContext }}</span>
                                        </div>

                                        <div class="flex justify-between">
                                            <span>Tidak DItemukan</span>
                                            <span class="text-red-500 font-bold">{{ $knowledgeGap }}</span>
                                        </div>

                                        <div class="flex justify-between">
                                            <span>Jawaban Kurang Tepat</span>
                                            <span class="text-yellow-500 font-bold">{{ $outOfContext }}</span>
                                        </div>

                                        <div class="flex justify-between">
                                            <span>Accuracy</span>
                                            <span class="text-blue-600 font-bold">{{ round($accuracy) }}%</span>
                                        </div>

                                        <div class="border-t pt-2 flex justify-between font-bold">
                                            <span>Total</span>
                                            <span>{{ $totalChats }}</span>
                                        </div>

                                    </div>

                                </div>

                                <div class="bg-indigo-50 text-indigo-800 p-3 rounded text-xs">
                                    💡 Cara Penilaian:
                                    <br>• Jawaban "maaf / tidak tahu" dianggap belum bisa menjawab
                                    <br>• Jawaban lainnya akan dicek kembali oleh admin
                                </div>
                            </div>

                        </div>

                    </div>
                </div>

            </div>

        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {

            const ctx = document.getElementById('aiPerformanceChart');

            new Chart(ctx, {
                type: 'doughnut',
                data: {
                    datasets: [{
                        data: [{{ $validContext }}, {{ $knowledgeGap }}, {{ $outOfContext }}],
                        backgroundColor: ['#10b981', '#ef4444', '#f59e0b'],
                        borderWidth: 0
                    }]
                },
                options: {
                    cutout: '75%',
                    plugins: {
                        legend: {
                            display: false
                        }
                    }
                }
            });

        });
    </script>

@endsection
