<?php
/**
 * Add Sample Timetable Entry
 * 
 * This script adds a sample timetable entry for testing the VoIP notification system
 */

require_once('config/database.php');

// Check if required parameters exist
$teachers = $pdo->query("SELECT id, first_name, last_name FROM teachers LIMIT 1")->fetchAll();
$classes = $pdo->query("SELECT id, name, section FROM classes LIMIT 1")->fetchAll();
$subjects = $pdo->query("SELECT id, name, code FROM subjects LIMIT 1")->fetchAll();
$sessions = $pdo->query("SELECT session_name FROM academic_sessions LIMIT 1")->fetchAll();

if (empty($teachers) || empty($classes) || empty($subjects) || empty($sessions)) {
    die("Error: Need at least one teacher, class, subject, and session to create a sample timetable entry.\n");
}

$sample_teacher = $teachers[0];
$sample_class = $classes[0];
$sample_subject = $subjects[0];
$sample_session = $sessions[0];

// Check if a similar entry already exists to avoid duplicates
$checkStmt = $pdo->prepare("
    SELECT id FROM timetable 
    WHERE teacher_id = ? AND class_id = ? AND subject_id = ? AND day_of_week = ? 
    AND start_time = ? AND end_time = ?
");
$checkStmt->execute([
    $sample_teacher['id'], 
    $sample_class['id'], 
    $sample_subject['id'], 
    'wednesday', 
    '10:00:00', 
    '11:00:00'
]);
$existing = $checkStmt->fetch();

if ($existing) {
    echo "Sample timetable entry already exists with ID: {$existing['id']}\n";
    exit(0);
}

// Insert a sample timetable entry for today (Wednesday) at 10:00 AM - 11:00 AM
$insertStmt = $pdo->prepare("
    INSERT INTO timetable (class_id, subject_id, teacher_id, day_of_week, start_time, end_time, room_number, academic_year, status)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
");

$result = $insertStmt->execute([
    $sample_class['id'],           // class_id
    $sample_subject['id'],         // subject_id
    $sample_teacher['id'],         // teacher_id
    'wednesday',                   // day_of_week (today)
    '10:00:00',                   // start_time
    '11:00:00',                   // end_time
    'Room A101',                  // room_number
    $sample_session['session_name'], // academic_year
    'active'                       // status
]);

if ($result) {
    $newId = $pdo->lastInsertId();
    echo "Sample timetable entry created successfully with ID: $newId\n";
    echo "Teacher: {$sample_teacher['first_name']} {$sample_teacher['last_name']} (ID: {$sample_teacher['id']})\n";
    echo "Class: {$sample_class['name']} {$sample_class['section']} (ID: {$sample_class['id']})\n";
    echo "Subject: {$sample_subject['name']} ({$sample_subject['code']}) (ID: {$sample_subject['id']})\n";
    echo "Day: Wednesday\n";
    echo "Time: 10:00 AM - 11:00 AM\n";
    echo "Session: {$sample_session['session_name']}\n";
    echo "\nThe VoIP notification system will detect this class when it runs at 10:00 AM on Wednesday.\n";
} else {
    echo "Failed to create sample timetable entry.\n";
}

?>