<?php
require_once __DIR__ . '/auth.php'; // Corrected path

// Require login
if (!is_logged_in()) {
    header('Location: login.php');
    exit();
}

$session_id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$session_id) {
    header('Location: training_sessions.php');
    exit();
}

$coach_id = get_user_id();
require_once __DIR__ . '/db/config.php'; // Ensure db() is available
$pdo = db();

// Verify the session belongs to the current coach before deleting
$stmt = $pdo->prepare("SELECT id FROM training_sessions WHERE id = ? AND coach_id = ?");
$stmt->execute([$session_id, $coach_id]);
$session = $stmt->fetch();

if ($session) {
    // The ON DELETE CASCADE on the training_session_drills table will handle deleting the links.
    $stmt = $pdo->prepare("DELETE FROM training_sessions WHERE id = ?");
    $stmt->execute([$session_id]);
}

// Redirect back to the list with a status message
header('Location: training_sessions.php?status=deleted');
exit();
?>