<?php
session_start();
include 'db_connection.php';

/* ================= AUTH ================= */
if (!isset($_SESSION['student_id']) || !isset($_SESSION['class_id'])) {
    header("Location: student_login.php");
    exit();
}

$class_id = $_SESSION['class_id'];

/* ================= TIMETABLE QUERY (FIXED SCHEMA) ================= */
$sql = "SELECT 
            t.day,
            t.start_time,
            t.end_time,
            s.subject_name,
            u.name AS teacher_name,
            t.location
        FROM timetable t
        JOIN subject s ON t.subject_id = s.subject_id
        JOIN users u ON t.teacher_id = u.user_id
        WHERE t.class_id = ?
        ORDER BY FIELD(t.day,'Monday','Tuesday','Wednesday','Thursday','Friday'), t.start_time";

$stmt = $conn->prepare($sql);

if (!$stmt) {
    die("SQL Error: " . $conn->error);
}

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

/* ================= STRUCTURE DATA ================= */
$timetable = [];
$time_slots = [];

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

    // FIX: using real schema fields (start_time + end_time)
    $time_slot = date("H:i", strtotime($row['start_time'])) 
               . " - " . 
               date("H:i", strtotime($row['end_time']));

    $timetable[$day][$time_slot] = [
        'subject' => $row['subject_name'],
        'teacher' => $row['teacher_name'],
        'location' => $row['location']
    ];

    $time_slots[] = $time_slot;
}

$time_slots = array_values(array_unique($time_slots));
sort($time_slots);

$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Class Timetable</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<style>
body {
    background: #f4f6f9;
    font-family: "Segoe UI", sans-serif;
}

/* HEADER */
.header {
    background: linear-gradient(135deg, #2c3e50, #34495e);
    color: white;
    padding: 20px;
    border-radius: 12px;
    text-align: center;
    margin-bottom: 20px;
}

/* TABLE CONTAINER */
.table-box {
    background: white;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    overflow-x: auto;
}

/* TABLE */
table {
    width: 100%;
    border-collapse: collapse;
    min-width: 800px;
}

th {
    background: #2c3e50;
    color: white;
    padding: 12px;
    text-align: center;
    position: sticky;
    top: 0;
}

td {
    border: 1px solid #eee;
    padding: 12px;
    text-align: center;
    vertical-align: top;
}

td:hover {
    background: #f8f9fa;
}

/* SUBJECT BOX */
.subject-box {
    background: #e8f0ff;
    padding: 10px;
    border-radius: 10px;
    font-size: 14px;
}

/* DAY COLUMN */
.day-col {
    font-weight: bold;
    background: #f1f3f5;
}

/* EMPTY */
.empty {
    color: #aaa;
}
</style>
</head>

<body>

<div class="container mt-4">

    <!-- HEADER -->
    <div class="header">
        <h3><i class="fas fa-calendar-alt"></i> My Class Timetable</h3>
        <p class="mb-0">Weekly academic schedule</p>
    </div>

    <?php if (!empty($timetable)): ?>

    <div class="table-box">

        <table>
            <thead>
                <tr>
                    <th>Day</th>
                    <?php foreach ($time_slots as $slot): ?>
                        <th><?php echo htmlspecialchars($slot); ?></th>
                    <?php endforeach; ?>
                </tr>
            </thead>

            <tbody>
                <?php foreach (['Monday','Tuesday','Wednesday','Thursday','Friday'] as $day): ?>
                    <tr>
                        <td class="day-col"><?php echo $day; ?></td>

                        <?php foreach ($time_slots as $slot): ?>
                            <td>
                                <?php if (isset($timetable[$day][$slot])): 
                                    $t = $timetable[$day][$slot]; ?>

                                    <div class="subject-box">
                                        <strong><?php echo htmlspecialchars($t['subject']); ?></strong><br>

                                        
                                    </div>

                                <?php else: ?>
                                    <span class="empty">---</span>
                                <?php endif; ?>
                            </td>
                        <?php endforeach; ?>

                    </tr>
                <?php endforeach; ?>
            </tbody>

        </table>

    </div>

    <?php else: ?>
        <div class="alert alert-warning text-center">
            No timetable available for this class.
        </div>
    <?php endif; ?>

</div>

</body>
</html>