<?php
/**
 * api/auto_alert.php
 * Notifikasi Otomatis ke Email Admin
 */
include '../config/database.php';

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

// Cek Status Gemini
$ch = curl_init("https://generativelanguage.googleapis.com/v1beta/models?key=" . $cfg['gemini_api_key']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Jika Limit (429) atau Invalid (401)
if ($http_code !== 200) {
    $to = "sikawarsito.s.kom@gmail.com"; // GANTI DENGAN EMAIL BAPAK
    $subject = "ALERT: API AI SMK Bermasalah!";
    $message = "Halo Admin,\n\nSistem mendeteksi bahwa API Key Gemini Anda mengalami gangguan dengan Kode Respons: " . $http_code . ".\n\nMohon segera lakukan pembaruan di Panel Konfigurasi.\n\nSalam,\nSistem Monitoring Otomatis.";
    $headers = "From: no-reply@sekolahbapak.sch.id";

    // Kirim Email
    mail($to, $subject, $message, $headers);
}
?>