<?php
session_start();
require_once 'includes/database.php';
require_once 'Classes/Timetable.php';

header('Content-Type: application/json');

if(!isset($_SESSION['username'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

$timetable = new Timetable();

$data = [
    'period_name' => $_POST['period_name'],
    'period_number' => $_POST['period_number'],
    'start_time' => $_POST['start_time'],
    'end_time' => $_POST['end_time'],
    'duration' => $_POST['duration'],
    'is_break' => isset($_POST['is_break']) ? 1 : 0
];

try {
    if($timetable->createPeriod($data)) {
        echo json_encode(['success' => true, 'message' => 'Period created successfully']);
    } else {
        echo json_encode(['success' => false, 'message' => 'Failed to create period']);
    }
} catch(Exception $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
