<?php
session_start();
header('Content-Type: application/json');
require_once '../DB/db_credentials.php';
require_once '../DB/db_connection.php';
// global $dbc; // needs to be set globally so $dbc is accessible in this file too
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$email = $_SESSION["email"];
$userID = $_SESSION["userID"];
$filmID = $_POST["filmID"];
$start = $_POST["start"];
// get film duration
$query = "SELECT duration FROM citeLeParis_film WHERE pk_filmID = ?";
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "i", $filmID);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $duration);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
if (!$duration) {
echo json_encode(["status" => "error", "message" => "Film not found."]);
exit;
}
// calculate endTime manually
$startTimestamp = strtotime($start);
$endTimestamp = $startTimestamp + ($duration * 60);
$startTimeFormatted = date("Y-m-d H:i:s", $startTimestamp);
$endTimeFormatted = date("Y-m-d H:i:s", $endTimestamp);
// insert into schedule table
$query = "INSERT INTO citeLeParis_schedule (fk_userID, fk_filmID, startTime, endTime, reminderSent) VALUES (?, ?, ?, ?, 0)";
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "iiss", $userID, $filmID, $startTimeFormatted, $endTimeFormatted);
mysqli_stmt_execute($stmt);
$scheduleID = mysqli_insert_id($dbc);
mysqli_stmt_close($stmt);
echo json_encode(['status' => 'success', 'scheduleID' => $scheduleID]);
}
?>