<?php
require_once 'includes/header-siswa.php';
checkSiswa();

if (!isset($_SESSION['last_result'])) {
    redirect('diagnosa.php');
}

$results = $_SESSION['last_result'];
$riwayat_id = $_SESSION['riwayat_id'];

// Get top result
$top = reset($results);
$interpretasi = getKategoriCF($pdo, $top['id'], $top['cf']);

// Get symptoms used in this session for display
$stmt = $pdo->prepare("
    SELECT g.nama, j.cf_user 
    FROM jawaban_user j
    JOIN gejala g ON j.id_gejala = g.id
    WHERE j.id_riwayat = ?
");
$stmt->execute(array($riwayat_id));
$jawaban = $stmt->fetchAll();
?>

<div class="container mx-auto px-6 py-12">
    <div class="max-w-4xl mx-auto">
        <div class="text-center mb-12">
            <h2 class="text-4xl font-extrabold text-slate-800 mb-4">Hasil Diagnosa</h2>
            <p class="text-slate-500 font-medium">Berdasarkan gejala yang Anda pilih, berikut adalah analisis sistem:</p>
        </div>

        <!-- Result Card -->
        <div class="card p-10 md:p-16 border-none shadow-2xl relative overflow-hidden mb-8">
            <!-- Background accent -->
            <div class="absolute top-0 right-0 w-64 h-64 bg-primary/5 rounded-full -mr-20 -mt-20 blur-3xl"></div>
            
            <div class="relative z-10 text-center">
                <p class="text-sm font-bold text-primary uppercase tracking-widest mb-4">Diagnosis Utama (Kesimpulan Akhir)</p>
                <?php 
                $final = null;
                $breakdown = array();
                foreach ($results as $res) {
                    if ($res['tipe'] === 'FINAL') $final = $res;
                    else $breakdown[] = $res;
                }
                
                // Fetch Solution for Top Result
                $target_diag = $final ? $final : $top;
                $target_cf_rata = isset($target_diag['cf_rata']) ? $target_diag['cf_rata'] : 0;
                $stmt_solusi = $pdo->prepare("SELECT saran FROM solusi WHERE id_diagnosa = ? AND ? BETWEEN min_cf AND max_cf LIMIT 1");
                $stmt_solusi->execute(array($target_diag['id'], $target_cf_rata));
                $saran = $stmt_solusi->fetchColumn();
                ?>
                
                <?php 
                $display_diag = $final ? $final : $top;
                $display_cf = isset($display_diag['cf']) ? $display_diag['cf'] : 0;
                $display_cf_rata = isset($display_diag['cf_rata']) ? $display_diag['cf_rata'] : 0;
                $cat_murni = getKategoriCF($pdo, $display_diag['id'], $display_cf);
                $cat_rata = getKategoriCF($pdo, $display_diag['id'], $display_cf_rata);
                ?>
                <h3 class="text-3xl md:text-5xl font-extrabold text-slate-800 mb-8 leading-tight"><?= $display_diag['nama'] ?></h3>
                
                <div class="grid grid-cols-1 gap-6 mb-8 max-w-sm mx-auto text-left">
                    <div class="bg-slate-50 p-6 rounded-2xl border border-slate-200 shadow-sm flex flex-col justify-center items-center text-center">
                        <p class="text-[10px] font-bold text-slate-400 uppercase tracking-widest mb-2">Metode Asli (Tingkat Keyakinan)</p>
                        <div class="text-4xl md:text-5xl font-black text-slate-700 mb-2"><?= number_format($display_cf * 100, 2) ?><span class="text-2xl">%</span></div>
                        <p class="text-sm font-bold text-slate-500">Kategori: <?= $cat_murni ?></p>
                    </div>
                </div>

                <?php if ($saran): ?>
                    <div class="max-w-2xl mx-auto mb-10 p-6 bg-primary/5 rounded-2xl border border-primary/10">
                        <p class="text-[10px] font-black text-primary uppercase tracking-[0.2em] mb-3">Saran & Solusi:</p>
                        <p class="text-slate-700 font-medium leading-relaxed"><?= $saran ?></p>
                    </div>
                <?php endif; ?>
                
                <div class="flex flex-wrap justify-center gap-6 mt-8 p-6 bg-slate-50 rounded-2xl border border-slate-100">
                    <?php foreach ($breakdown as $br): ?>
                        <div class="text-center px-6 border-r border-slate-200 last:border-none">
                            <p class="text-[10px] font-bold text-slate-400 uppercase tracking-widest mb-1"><?= $br['nama'] ?></p>
                            <p class="text-lg font-black text-slate-700"><?= number_format($br['cf'] * 100, 2) ?>%</p>
                            <p class="text-[10px] text-slate-400 font-medium"><?= getKategoriCF($pdo, $br['id'], $br['cf']) ?></p>
                        </div>
                    <?php endforeach; ?>
                </div>

                <div class="mt-8">
                    <a href="detail-perhitungan.php" class="inline-flex items-center gap-2 text-sm font-bold text-primary hover:text-primary-dark transition-all">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17v-2a4 4 0 00-4-4H5m11 2v2a4 4 0 004 4h1m-6-4h.01M9 16h.01"></path></svg>
                        Lihat Detail Perhitungan Matematika
                    </a>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            <!-- Symptom Detail -->
            <div class="card p-8">
                <h4 class="text-lg font-bold text-slate-800 mb-6">Gejala yang Dirasakan:</h4>
                <ul class="space-y-4">
                    <?php foreach ($jawaban as $jw): ?>
                        <li class="flex items-start gap-4 text-sm">
                            <div class="w-5 h-5 rounded-full bg-green-100 text-green-600 flex items-center justify-center flex-shrink-0 mt-0.5">
                                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path></svg>
                            </div>
                            <div class="flex-1">
                                <p class="text-slate-700 font-bold"><?= $jw['nama'] ?></p>
                                <p class="text-xs text-slate-400">Keyakinan Anda: <?= round($jw['cf_user'] * 100) ?>%</p>
                            </div>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>

            <!-- Recommendation (Mockup but logic-ready) -->
            <div class="card p-8 bg-primary text-white border-none">
                <h4 class="text-lg font-bold mb-6">Rekomendasi Awal:</h4>
                <p class="text-sm opacity-90 leading-relaxed mb-6">
                    Jangan merasa sendirian. Langkah pertama untuk merasa lebih baik adalah dengan berbicara kepada seseorang yang Anda percayai.
                </p>
                <ul class="space-y-4 text-sm font-medium">
                    <li class="flex items-center gap-3">
                        <div class="w-8 h-8 rounded-full bg-white/10 flex items-center justify-center">1</div>
                        Hubungi Guru BK SMAN 1 Barumun
                    </li>
                    <li class="flex items-center gap-3">
                        <div class="w-8 h-8 rounded-full bg-white/10 flex items-center justify-center">2</div>
                        Lakukan teknik pernapasan dalam
                    </li>
                    <li class="flex items-center gap-3">
                        <div class="w-8 h-8 rounded-full bg-white/10 flex items-center justify-center">3</div>
                        Istirahat yang cukup di malam hari
                    </li>
                </ul>
                <a href="<?= baseUrl() ?>" class="block w-full text-center py-4 bg-accent text-primary font-bold rounded-xl mt-10 hover:shadow-lg transition-all">Kembali ke Beranda</a>
            </div>
        </div>
    </div>
</div>

<?php require_once 'includes/footer-siswa.php'; ?>
