<?php
require_once __DIR__ . '/partials/header.php';

// 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();
$pdo = db();

// Fetch the training session
$stmt = $pdo->prepare("SELECT * FROM training_sessions WHERE id = ? AND coach_id = ?");
$stmt->execute([$session_id, $coach_id]);
$session = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$session) {
    // Or show a 404 not found page
    header('Location: training_sessions.php');
    exit();
}

// Fetch the drills for this session, in order
$stmt = $pdo->prepare(
    "SELECT d.* 
     FROM drills d
     JOIN training_session_drills tsd ON d.id = tsd.drill_id
     WHERE tsd.session_id = ? "
);
$stmt->execute([$session_id]);
$drills = $stmt->fetchAll(PDO::FETCH_ASSOC);

$pageTitle = htmlspecialchars($session['name']);
$pageDescription = htmlspecialchars(substr($session['description'], 0, 150));

?>

<main class="container my-5">
    <div class="row">
        <div class="col-lg-10 mx-auto">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div>
                    <h1 class="h2"><?php echo $pageTitle; ?></h1>
                    <p class="text-muted"><?php echo $pageDescription; ?></p>
                </div>
                <div>
                    <a href="edit_training_session.php?id=<?php echo $session['id']; ?>" class="btn btn-outline-secondary me-2">Edit</a>
                    <a href="delete_training_session.php?id=<?php echo $session['id']; ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this session?');">Delete</a>
                    <a href="export_training_session_pdf.php?id=<?php echo $session['id']; ?>" class="btn btn-outline-primary">Export to PDF</a>
                </div>
            </div>

            <div class="card shadow-sm border-0 rounded-4">
                <div class="card-header bg-light p-3">
                    <h5 class="mb-0">Drills in this Session</h5>
                </div>
                <div class="card-body">
                    <?php if (empty($drills)): ?>
                        <p class="text-center text-muted m-3">This training session has no drills.</p>
                    <?php else: ?>
                        <div class="accordion" id="drills-accordion">
                            <?php foreach ($drills as $index => $drill): ?>
                                <div class="accordion-item">
                                    <h2 class="accordion-header" id="heading-<?php echo $drill['id']; ?>">
                                        <button class="accordion-button <?php echo $index > 0 ? 'collapsed' : ''; ?>" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-<?php echo $drill['id']; ?>" aria-expanded="<?php echo $index === 0 ? 'true' : 'false'; ?>" aria-controls="collapse-<?php echo $drill['id']; ?>">
                                            <strong><?php echo htmlspecialchars($drill['title']); ?></strong>&nbsp; (<?php echo $drill['duration_minutes'] ?> mins)
                                        </button>
                                    </h2>
                                    <div id="collapse-<?php echo $drill['id']; ?>" class="accordion-collapse collapse <?php echo $index === 0 ? 'show' : ''; ?>" aria-labelledby="heading-<?php echo $drill['id']; ?>" data-bs-parent="#drills-accordion">
                                        <div class="accordion-body">
                                            <p><?php echo nl2br(htmlspecialchars($drill['description'])); ?></p>
                                            <a href="drill.php?id=<?php echo $drill['id']; ?>" class="btn btn-sm btn-outline-primary">View Full Drill Details</a>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</main>

<?php require_once __DIR__ . '/partials/footer.php'; ?>
