<?php
require_once __DIR__ . '/partials/header.php';
require_once 'db/config.php';
require_once 'auth.php';

$drill = null;
if (isset($_GET['id'])) {
    try {
        $pdo = db();
        $stmt = $pdo->prepare("SELECT * FROM drills WHERE id = ?");
        $stmt->execute([$_GET['id']]);
        $drill = $stmt->fetch(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        // For production, you would log this error and show a generic error page.
        die("Error fetching drill details: " . $e->getMessage());
    }
}

if (!$drill) {
    http_response_code(404);
    // A simple 404 page, you can create a more styled one.
    die('Drill not found.');
}

$pageTitle = htmlspecialchars($drill['title']);
$pageDescription = htmlspecialchars(substr($drill['description'], 0, 160));

?>

        <main class="container mt-5">
            <?php if (isset($_GET['created']) && $_GET['created'] == 'true') : ?>
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <strong>Success!</strong> Your drill has been created successfully.
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php endif; ?>

            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="fw-bold"><?php echo htmlspecialchars($drill['title']); ?></h1>
                <?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == $drill['user_id']) : ?>
                    <div>
                        <a href="edit_drill.php?id=<?php echo $drill['id']; ?>" class="btn btn-outline-primary btn-sm">Edit</a>
                        <a href="delete_drill.php?id=<?php echo $drill['id']; ?>" class="btn btn-outline-danger btn-sm" onclick="return confirm('Are you sure you want to delete this drill?');">Delete</a>
                        <a href="export_drill_pdf.php?id=<?php echo $drill['id']; ?>" class="btn btn-outline-secondary btn-sm">Export to PDF</a>
                    </div>
                <?php endif; ?>
            </div>

            <div class="row">
                <!-- Left Column: Details & Instructions -->
                <div class="col-lg-6">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header">
                            <h5 class="mb-0">Drill Details</h5>
                        </div>
                        <div class="card-body">
                            <ul class="list-group list-group-flush">
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <strong>Players:</strong>
                                    <span><?php echo htmlspecialchars($drill['min_players']) . ' - ' . htmlspecialchars($drill['max_players']); ?></span>
                                </li>
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <strong>Age Group:</strong>
                                    <span class="badge bg-primary rounded-pill"><?php echo htmlspecialchars($drill['age_group']); ?></span>
                                </li>
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <strong>Skill Focus:</strong>
                                    <span><?php echo htmlspecialchars($drill['skill_focus']); ?></span>
                                </li>
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <strong>Difficulty:</strong>
                                    <span class="badge bg-danger rounded-pill"><?php echo htmlspecialchars($drill['difficulty']); ?></span>
                                </li>
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <strong>Duration:</strong>
                                    <span><?php echo htmlspecialchars($drill['duration_minutes']); ?> minutes</span>
                                </li>
                                <li class="list-group-item">
                                    <strong>Equipment:</strong>
                                    <p class="mb-0"><?php echo htmlspecialchars($drill['equipment_required']); ?></p>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="card shadow-sm mb-4">
                        <div class="card-body">
                            <h3 class="card-title">Instructions</h3>
                            <p class="lead"><?php echo nl2br(htmlspecialchars($drill['description'])); ?></p>
                        </div>
                    </div>
                </div>

                <!-- Right Column: Media (Image/Video) -->
                <div class="col-lg-6">
                    <?php if (!empty($drill['image_path'])) : ?>
                        <div class="mb-4 shadow-sm rounded">
                            <img src="<?php echo htmlspecialchars($drill['image_path']); ?>" class="img-fluid rounded" alt="Drill Image">
                        </div>
                    <?php endif; ?>

                    <?php if (!empty($drill['youtube_url']) && get_youtube_id_from_url($drill['youtube_url'])) : ?>
                        <div class="video-container mb-4 shadow-sm rounded overflow-hidden">
                            <iframe src="https://www.youtube.com/embed/<?php echo get_youtube_id_from_url($drill['youtube_url']); ?>?rel=0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </main>

    



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