<?php
// File: api/get_jadwal.php
header('Content-Type: application/json');
require_once '../config/database.php';

$hari = $_GET['hari'] ?? '';
$jam = $_GET['jam'] ?? '';

if (!$hari || !$jam) {
    echo json_encode(['found' => false]);
    exit;
}

// Cek apakah sudah dibunyikan hari ini
$logCheck = $conn->prepare("SELECT id FROM belloto_log WHERE tanggal = CURDATE() AND jam = ?");
$logCheck->bind_param("s", $jam);
$logCheck->execute();
$logResult = $logCheck->get_result();
$already_rung = $logResult->num_rows > 0;

// Ambil jadwal dari tabel belloto_jadwal
$stmt = $conn->prepare("SELECT * FROM belloto_jadwal WHERE hari = ? AND jam = ? AND aktif = 1");
$stmt->bind_param("ss", $hari, $jam);
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    echo json_encode([
        'found' => true,
        'already_rung' => $already_rung,
        'kegiatan' => $row['nama_kegiatan'],
        'audio_file' => $row['audio_file']
    ]);
} else {
    echo json_encode(['found' => false]);
}

$conn->close();
?>