<?php
session_start();
if (!isset($_SESSION['admin'])) {
    header('Location: login.php');
    exit();
}
include 'db.php';

$message = '';

// Handle adding a new quiz game
if (isset($_POST['add_quiz'])) {
    $quiz_name = $_POST['quiz_name'];
    $description = $_POST['description'];
    $game_id = $_POST['game_id']; // This links the quiz to a 'game' entry in the games table

    if ($stmt = $conn->prepare("INSERT INTO quiz_games (game_id, quiz_name, description) VALUES (?, ?, ?)")) {
        $stmt->bind_param("iss", $game_id, $quiz_name, $description);
        if ($stmt->execute()) {
            $message = "<div class='alert alert-success'>Quiz created successfully!</div>";
        } else {
            $message = "<div class='alert alert-danger'>Error: " . $stmt->error . "</div>";
        }
        $stmt->close();
    }
}

// Fetch all quizzes
$quizzesQuery = "SELECT qg.*, g.game_name FROM quiz_games qg LEFT JOIN games g ON qg.game_id = g.game_id ORDER BY qg.quiz_id DESC";
$quizzesResult = $conn->query($quizzesQuery);

// Fetch games for selection
$gamesResult = $conn->query("SELECT game_id, game_name FROM games ORDER BY game_name ASC");

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Quizzes - GameHub Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background-color: #f0f4f8;
        }

        .sidebar {
            height: 100vh;
            background: #2c3e50;
            color: white;
            padding-top: 20px;
            position: fixed;
            width: 250px;
        }

        .sidebar a {
            color: #bdc3c7;
            text-decoration: none;
            display: block;
            padding: 10px 20px;
        }

        .sidebar a:hover,
        .sidebar a.active {
            background: #34495e;
            color: white;
        }

        .main-content {
            margin-left: 250px;
            padding: 40px;
        }

        .card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .quiz-card {
            transition: transform 0.2s;
        }

        .quiz-card:hover {
            transform: translateY(-5px);
        }
    </style>
</head>

<body>
    <div class="sidebar">
        <h3 class="text-center mb-4">GameHub Admin</h3>
        <a href="admin.php"><i class="fas fa-home me-2"></i> Dashboard</a>
        <a href="games.php"><i class="fas fa-gamepad me-2"></i> Games</a>
        <a href="manage_quizzes.php" class="active"><i class="fas fa-lightbulb me-2"></i> Quizzes</a>
        <a href="manage_videos.php"><i class="fas fa-video me-2"></i> Video Feed</a>
        <a href="events.php"><i class="fas fa-calendar-alt me-2"></i> Events</a>
        <a href="logout.php"><i class="fas fa-sign-out-alt me-2"></i> Logout</a>
    </div>

    <div class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-lightbulb text-primary me-2"></i> Quiz Management</h2>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addQuizModal">
                <i class="fas fa-plus me-2"></i> Create New Quiz
            </button>
        </div>

        <?php echo $message; ?>

        <!-- Quizzes List -->
        <div class="row">
            <?php if ($quizzesResult && $quizzesResult->num_rows > 0): ?>
                <?php while ($row = $quizzesResult->fetch_assoc()): ?>
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card quiz-card h-100">
                            <div class="card-body">
                                <h5 class="card-title">
                                    <?php echo htmlspecialchars($row['quiz_name']); ?>
                                </h5>
                                <h6 class="card-subtitle mb-2 text-muted">Game:
                                    <?php echo htmlspecialchars($row['game_name'] ?? 'None'); ?>
                                </h6>
                                <p class="card-text text-truncate">
                                    <?php echo htmlspecialchars($row['description']); ?>
                                </p>
                                <div class="d-grid gap-2 mt-3">
                                    <a href="manage_questions.php?quiz_id=<?php echo $row['quiz_id']; ?>"
                                        class="btn btn-outline-primary btn-sm">
                                        <i class="fas fa-list-ol me-1"></i> Manage Questions
                                    </a>
                                    <div class="btn-group">
                                        <a href="edit_quiz.php?id=<?php echo $row['quiz_id']; ?>"
                                            class="btn btn-outline-secondary btn-sm">Edit</a>
                                        <a href="quiz_results.php?quiz_id=<?php echo $row['quiz_id']; ?>"
                                            class="btn btn-outline-info btn-sm text-dark">Results</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="col-12">
                    <div class="alert alert-info">No quizzes found. Start by creating one!</div>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Add Quiz Modal -->
    <div class="modal fade" id="addQuizModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title">Create New Quiz</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">Quiz Name</label>
                            <input type="text" name="quiz_name" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Associated Game</label>
                            <select name="game_id" class="form-select" required>
                                <option value="">Select a game...</option>
                                <?php if ($gamesResult): ?>
                                    <?php
                                    $gamesResult->data_seek(0);
                                    while ($game = $gamesResult->fetch_assoc()):
                                        ?>
                                        <option value="<?php echo $game['game_id']; ?>">
                                            <?php echo htmlspecialchars($game['game_name']); ?>
                                        </option>
                                    <?php endwhile; ?>
                                <?php endif; ?>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Description</label>
                            <textarea name="description" class="form-control" rows="3"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" name="add_quiz" class="btn btn-primary">Create Quiz</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>