<?php
// FILE DIAGNOSA (HANYA UNTUK DEBUGGING)
error_reporting(E_ALL);
ini_set('display_errors', 1);

include '../config/database.php';

echo "<h1>🔍 DIAGNOSA SISTEM AI</h1>";
echo "<hr>";

// 1. CEK KONEKSI DATABASE
echo "<h3>1. Cek Database & Config</h3>";
if (!$conn) {
    die("<span style='color:red'>❌ Koneksi Database Gagal!</span>");
} else {
    echo "<span style='color:green'>✅ Database Terhubung.</span><br>";
}

$res = mysqli_query($conn, "SELECT * FROM settings WHERE id = 1");
$cfg = mysqli_fetch_assoc($res);

// 2. CEK API KEY
echo "<h3>2. Cek API Key</h3>";
$g_key = $cfg['gemini_api_key'];
$groq_keys = array_filter([
    $cfg['groq_api_key'], $cfg['groq_api_key_2'], 
    $cfg['groq_api_key_3'], $cfg['groq_api_key_4']
]);

if (empty($g_key)) {
    echo "<span style='color:red'>❌ Gemini Key: KOSONG!</span><br>";
} else {
    echo "<span style='color:green'>✅ Gemini Key: Terdeteksi (" . substr($g_key, 0, 5) . "...)</span><br>";
}

if (empty($groq_keys)) {
    echo "<span style='color:red'>❌ Groq Keys: KOSONG SEMUA! (Pastikan minimal isi 1 key di database)</span><br>";
} else {
    echo "<span style='color:green'>✅ Groq Keys: " . count($groq_keys) . " Key ditemukan.</span><br>";
}

// 3. TES PANGGIL GEMINI (RAW DEBUG)
echo "<h3>3. Simulasi Panggil Gemini</h3>";
$url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" . $g_key;
$data = [
    "contents" => [
        ["parts" => [["text" => "Jawab 'Halo' saja."]]]
    ]
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Bypass SSL
curl_setopt($ch, CURLOPT_VERBOSE, true); // Aktifkan Verbose
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error_curl = curl_error($ch);
curl_close($ch);

echo "<b>HTTP Status:</b> $http_code<br>";
if ($http_code == 200) {
    echo "<span style='color:green'>✅ Gemini Berhasil Merespon!</span><br>";
    $json = json_decode($result, true);
    echo "Jawaban AI: " . ($json['candidates'][0]['content']['parts'][0]['text'] ?? 'JSON Error');
} else {
    echo "<span style='color:red'>❌ Gemini ERROR!</span><br>";
    echo "<b>Curl Error:</b> $error_curl<br>";
    echo "<b>Response Server:</b> $result<br>";
}

// 4. TES PANGGIL GROQ (RAW DEBUG)
echo "<h3>4. Simulasi Panggil Groq (Key Pertama)</h3>";
if (!empty($groq_keys)) {
    $first_groq = reset($groq_keys); // Ambil key pertama
    $ch = curl_init("https://api.groq.com/openai/v1/chat/completions");
    $data_groq = [
        "model" => "llama-3.3-70b-versatile",
        "messages" => [["role" => "user", "content" => "Halo"]]
    ];
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $first_groq", "Content-Type: application/json"]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_groq));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $res_groq = curl_exec($ch);
    $http_code_groq = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo "<b>HTTP Status:</b> $http_code_groq<br>";
    echo "<b>Response:</b> $res_groq<br>";
} else {
    echo "Skip Groq (Key Kosong).";
}
?>