<?php
session_start();

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

require_once '../db/config.php';

$pdo = db();
$stmt = $pdo->query('SELECT * FROM challenges ORDER BY created_at DESC');
$challenges = $stmt->fetchAll();

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Challenges</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container">
            <a class="navbar-brand" href="index.php">Admin Panel</a>
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                    <a class="nav-link" href="challenges.php">Challenges</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="competitions.php">Competitions</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="users.php">Users</a>
                </li>
            </ul>
            <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                <li class="nav-item">
                    <a class="nav-link" href="logout.php">Logout</a>
                </li>
            </ul>
        </div>
    </nav>

    <div class="container py-5">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1 class="display-4 fw-bold">Manage Challenges</h1>
            <a href="add_challenge.php" class="btn btn-primary">Add Challenge</a>
        </div>

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Title</th>
                    <th>Difficulty</th>
                    <th>Learning Style</th>
                    <th>Created At</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($challenges) > 0) : ?>
                    <?php foreach ($challenges as $challenge) : ?>
                        <tr>
                            <td><?php echo $challenge['id']; ?></td>
                            <td><?php echo htmlspecialchars($challenge['title']); ?></td>
                            <td><?php echo htmlspecialchars($challenge['difficulty']); ?></td>
                            <td><?php echo htmlspecialchars($challenge['learning_style']); ?></td>
                            <td><?php echo $challenge['created_at']; ?></td>
                            <td>
                                <a href="edit_challenge.php?id=<?php echo $challenge['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
                                <a href="delete_challenge.php?id=<?php echo $challenge['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this challenge?')">Delete</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else : ?>
                    <tr>
                        <td colspan="6" class="text-center">No challenges found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</body>
</html>
