<?php
session_start();
include 'includes/header.php';
require_once 'db/config.php';

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header('Location: competitions.php');
    exit();
}

$competition_id = $_GET['id'];

$pdo = db();
$stmt = $pdo->prepare('SELECT * FROM competitions WHERE id = ? AND deleted_at IS NULL');
$stmt->execute([$competition_id]);
$competition = $stmt->fetch();

if (!$competition) {
    header('Location: competitions.php');
    exit();
}

$user_id = $_SESSION['user_id'] ?? null;

$stmt = $pdo->prepare('SELECT * FROM competition_participants WHERE user_id = ? AND competition_id = ?');
$stmt->execute([$user_id, $competition['id']]);
$is_registered = $stmt->fetch();

$stmt = $pdo->prepare('SELECT u.name FROM competition_participants cp JOIN users u ON cp.user_id = u.id WHERE cp.competition_id = ?');
$stmt->execute([$competition_id]);
$participants = $stmt->fetchAll();

?>

<div class="container py-5">
    <?php if (isset($_SESSION['success_message'])) : ?>
        <div class="alert alert-success">
            <?php
            echo $_SESSION['success_message'];
            unset($_SESSION['success_message']);
            ?>
        </div>
    <?php endif; ?>

    <div class="row">
        <div class="col-md-8">
            <h1 class="display-4 fw-bold"><?php echo htmlspecialchars($competition['title']); ?></h1>
            <p class="fs-5 text-muted"><strong>Starts:</strong> <?php echo date('M d, Y', strtotime($competition['start_date'])); ?> | <strong>Ends:</strong> <?php echo date('M d, Y', strtotime($competition['end_date'])); ?></p>
            <hr>
            <ul class="nav nav-tabs" id="competitionTab" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="description-tab" data-bs-toggle="tab" data-bs-target="#description" type="button" role="tab" aria-controls="description" aria-selected="true">Description</button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="participants-tab" data-bs-toggle="tab" data-bs-target="#participants" type="button" role="tab" aria-controls="participants" aria-selected="false">Participants (<?php echo count($participants); ?>)</button>
                </li>
            </ul>
            <div class="tab-content" id="competitionTabContent">
                <div class="tab-pane fade show active" id="description" role="tabpanel" aria-labelledby="description-tab">
                    <div class="pt-4">
                        <h2>Description</h2>
                        <p><?php echo nl2br(htmlspecialchars($competition['description'])); ?></p>
                    </div>
                </div>
                <div class="tab-pane fade" id="participants" role="tabpanel" aria-labelledby="participants-tab">
                    <div class="pt-4">
                        <h2>Participants</h2>
                        <ul class="list-group">
                            <?php if (count($participants) > 0) : ?>
                                <?php foreach ($participants as $participant) : ?>
                                    <li class="list-group-item"><?php echo htmlspecialchars($participant['name']); ?></li>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <li class="list-group-item">No participants yet.</li>
                            <?php endif; ?>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Registration</h5>
                    <?php if ($is_registered) : ?>
                        <div class="alert alert-success">You are already registered for this competition.</div>
                    <?php else : ?>
                        <form action="register_competition.php" method="post">
                            <input type="hidden" name="competition_id" value="<?php echo $competition['id']; ?>">
                            <button type="submit" class="btn btn-primary btn-lg">Register for this Competition</button>
                        </form>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
