<?php
include 'db_connection.php';

$days = ['Monday','Tuesday','Wednesday','Thursday','Friday'];

$classes = $conn->query("SELECT class_id, class_name FROM classes");

while ($class = $classes->fetch_assoc()) {

    $class_id = $class['class_id'];

    echo "
    <div style='margin:40px auto;width:95%;font-family:Arial;'>

        <h2 style='text-align:center;color:#1e293b;margin-bottom:15px;'>
            {$class['class_name']} Timetable
        </h2>
    ";

    /* =========================
       FREQUENCY
    ========================= */
    $freqResult = $conn->query("
        SELECT subject_id, frequency
        FROM class_subject
        WHERE class_id = $class_id
    ");

    $frequency = [];
    while ($f = $freqResult->fetch_assoc()){
        $frequency[$f['subject_id']] = $f['frequency'];
    }

    /* =========================
       USED COUNT
    ========================= */
    $usedResult = $conn->query("
        SELECT subject_id, COUNT(*) AS total
        FROM timetable
        WHERE class_id = $class_id
        GROUP BY subject_id
    ");

    $used = [];
    while ($u = $usedResult->fetch_assoc()){
        $used[$u['subject_id']] = $u['total'];
    }

    /* =========================
       FETCH TIMETABLE
    ========================= */
    $stmt = $conn->prepare("
        SELECT 
            t.day, t.period, t.subject_id,
            s.subject_name,
            te.name AS teacher_name,
            t.start_time, t.end_time
        FROM timetable t
        JOIN subject s ON s.subject_id = t.subject_id
        JOIN teachers te ON te.teacher_id = t.teacher_id
        WHERE t.class_id = ?
    ");

    $stmt->bind_param("i", $class_id);
    $stmt->execute();
    $result = $stmt->get_result();

    $timetable = [];

    while ($row = $result->fetch_assoc()){
        $timetable[$row['day']][$row['period']] = $row;
    }

    /* =========================
       TABLE UI
    ========================= */
    echo "
    <table border='1' style='width:100%;border-collapse:collapse;text-align:center;'>

        <tr style='background:#1e293b;color:white;'>
            <th>Day / Period</th>
            <th>1</th><th>2</th><th>3</th>
            <th>4</th><th>5</th><th>6</th><th>7</th>
            
        </tr>
    ";

    foreach ($days as $day){

        echo "<tr>";
        echo "<td style='font-weight:bold;background:#f1f5f9;'>$day</td>";

        for ($p = 1; $p <= 7; $p++){

            /* BREAK */
            $breakStmt = $conn->prepare("
                SELECT title FROM break_periods 
                WHERE day=? AND period=? LIMIT 1
            ");
            $breakStmt->bind_param("si", $day, $p);
            $breakStmt->execute();
            $break = $breakStmt->get_result()->fetch_assoc();

            if ($break){

                echo "
                <td style='background:#ffe08a;font-weight:bold;'>
                    🍽 {$break['title']}
                </td>";

            } elseif (isset($timetable[$day][$p])){

                $data = $timetable[$day][$p];

                $subject_id = $data['subject_id'];

                $current  = $used[$subject_id] ?? 0;
                $required = $frequency[$subject_id] ?? 0;

                echo "
                <td>
                    <b>{$data['subject_name']}</b><br>
                    <small>{$data['teacher_name']}</small><br>
                    <small>{$data['start_time']} - {$data['end_time']}</small><br>
                    <span style='color:#2563eb;font-weight:bold;'>
                        {$current}/{$required}
                    </span>
                </td>";

            } else {

                echo "<td style='background:#f8fafc;color:#999;'>Free</td>";
            }
        }

        echo "</tr>";
    }

    echo "</table></div>";
}
?>