<?php
/**
 * Debug Timetable Data
 * 
 * This script displays the current timetable data to help understand what classes are scheduled
 */

require_once('config/database.php');

echo "=== TIMETABLE DATA DEBUG ===\n\n";

// Get all timetable entries
$stmt = $pdo->query("
    SELECT t.*, 
           c.name as class_name, 
           c.section as class_section,
           s.name as subject_name, 
           s.code as subject_code,
           te.first_name as teacher_first_name, 
           te.last_name as teacher_last_name,
           te.phone as teacher_phone
    FROM timetable t
    JOIN classes c ON t.class_id = c.id
    JOIN subjects s ON t.subject_id = s.id
    JOIN teachers te ON t.teacher_id = te.id
    ORDER BY t.day_of_week, t.start_time, t.class_id
");

$timetableEntries = $stmt->fetchAll();

echo "Total timetable entries: " . count($timetableEntries) . "\n\n";

if (count($timetableEntries) > 0) {
    echo "Timetable Entries:\n";
    echo "------------------\n";
    
    foreach ($timetableEntries as $entry) {
        echo "ID: {$entry['id']}\n";
        echo "Teacher: {$entry['teacher_first_name']} {$entry['teacher_last_name']} (ID: {$entry['teacher_id']})\n";
        echo "Class: {$entry['class_name']} {$entry['class_section']} (ID: {$entry['class_id']})\n";
        echo "Subject: {$entry['subject_name']} ({$entry['subject_code']})\n";
        echo "Day: {$entry['day_of_week']}\n";
        echo "Time: {$entry['start_time']} - {$entry['end_time']}\n";
        echo "Session: {$entry['academic_year']}\n";
        echo "Room: {$entry['room_number']}\n";
        echo "Status: {$entry['status']}\n";
        echo "---\n";
    }
} else {
    echo "No timetable entries found in the database.\n";
}

// Check specifically for the requested parameters
echo "\n=== SPECIFIC QUERY RESULTS ===\n";
echo "Looking for teacher ID 8, class ID 12, session ID 8:\n\n";

// First get the session name for session ID 8
$sessionStmt = $pdo->prepare("SELECT session_name FROM academic_sessions WHERE id = ?");
$sessionStmt->execute([8]);
$session = $sessionStmt->fetch();

if ($session) {
    echo "Session ID 8 corresponds to: {$session['session_name']}\n";
    
    // Now check for timetable entries matching these criteria
    $stmt = $pdo->prepare("
        SELECT t.*, 
               c.name as class_name, 
               c.section as class_section,
               s.name as subject_name, 
               s.code as subject_code,
               te.first_name as teacher_first_name, 
               te.last_name as teacher_last_name,
               te.phone as teacher_phone
        FROM timetable t
        JOIN classes c ON t.class_id = c.id
        JOIN subjects s ON t.subject_id = s.id
        JOIN teachers te ON t.teacher_id = te.id
        WHERE t.teacher_id = ? 
        AND t.class_id = ?
        AND t.academic_year = ?
        AND t.day_of_week = ?
        AND t.status = 'active'
    ");
    
    $currentDay = strtolower(date('l')); // e.g., 'monday', 'tuesday'
    $stmt->execute([8, 12, $session['session_name'], $currentDay]);
    $matchingEntries = $stmt->fetchAll();
    
    if (count($matchingEntries) > 0) {
        echo "\nMatching entries found for today ({$currentDay}):\n";
        foreach ($matchingEntries as $entry) {
            echo "Class: {$entry['class_name']} {$entry['class_section']}\n";
            echo "Subject: {$entry['subject_name']}\n";
            echo "Time: {$entry['start_time']} - {$entry['end_time']}\n";
            echo "Teacher: {$entry['teacher_first_name']} {$entry['teacher_last_name']}\n";
            echo "---\n";
        }
    } else {
        echo "\nNo matching entries found for teacher ID 8, class ID 12, session '{$session['session_name']}' on {$currentDay}.\n";
        echo "This explains why no notifications were sent.\n";
    }
} else {
    echo "Session ID 8 not found in the academic_sessions table.\n";
}

echo "\nCurrent time: " . date('Y-m-d H:i:s') . "\n";
echo "Current day: " . ucfirst(strtolower(date('l'))) . "\n";

?>