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


if(!isset($_SESSION['username'])) {
    header('Location: index');
    exit();
}

// Set headers for CSV download
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="timetable_bulk_import_template.csv"');

// Create file pointer
$output = fopen('php://output', 'w');

// Add CSV header
fputcsv($output, array('day_id', 'period_id', 'subject_id', 'teacher_id', 'room_number', 'notes'));

// Add example rows
fputcsv($output, array('1', '2', '5', '3', '101', 'Monday Period 2 - Mathematics'));
fputcsv($output, array('1', '4', '7', '5', '102', 'Monday Period 3 - English'));
fputcsv($output, array('2', '2', '5', '3', '101', 'Tuesday Period 2 - Mathematics'));

// Add instruction rows as comments
fputcsv($output, array('# Instructions:', '', '', '', '', ''));
fputcsv($output, array('# day_id: 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday', '', '', '', '', ''));
fputcsv($output, array('# period_id: Check your timetable_periods table for valid IDs', '', '', '', '', ''));
fputcsv($output, array('# subject_id: Check your subjects table for valid IDs', '', '', '', '', ''));
fputcsv($output, array('# teacher_id: Check your teachers table for valid IDs', '', '', '', '', ''));
fputcsv($output, array('# room_number: Optional - any room identifier', '', '', '', '', ''));
fputcsv($output, array('# notes: Optional - any additional notes', '', '', '', '', ''));
fputcsv($output, array('# Delete these instruction rows and example rows before importing', '', '', '', '', ''));

fclose($output);
exit();