<?php
session_start();

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

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

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

$challenge_id = $_GET['id'];

$pdo = db();
$stmt = $pdo->prepare('SELECT * FROM challenges WHERE id = ?');
$stmt->execute([$challenge_id]);
$challenge = $stmt->fetch();

if (!$challenge) {
    header('Location: challenges.php');
    exit();
}

$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['title'] ?? '';
    $description = $_POST['description'] ?? '';
    $difficulty = $_POST['difficulty'] ?? '';
    $learning_style = $_POST['learning_style'] ?? '';
    $expected_output = $_POST['expected_output'] ?? '';
    $sample_cases_json = $_POST['sample_cases_json'] ?? '';

    if (empty($title)) {
        $errors[] = 'Title is required';
    }
    if (empty($description)) {
        $errors[] = 'Description is required';
    }
    if (empty($difficulty)) {
        $errors[] = 'Difficulty is required';
    }

    if (empty($errors)) {
        $stmt = $pdo->prepare('UPDATE challenges SET title = ?, description = ?, difficulty = ?, learning_style = ?, expected_output = ?, sample_cases_json = ? WHERE id = ?');
        if ($stmt->execute([$title, $description, $difficulty, $learning_style, $expected_output, $sample_cases_json, $challenge_id])) {
            header('Location: challenges.php?success=true');
            exit;
        } else {
            $errors[] = 'Failed to update challenge. Please try again.';
        }
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Challenge</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">
        <h1 class="display-4 fw-bold">Edit Challenge</h1>

        <?php if (!empty($errors)): ?>
            <div class="alert alert-danger">
                <?php foreach ($errors as $error): ?>
                    <p><?php echo $error; ?></p>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <form action="edit_challenge.php?id=<?php echo $challenge_id; ?>" method="post">
            <div class="mb-3">
                <label for="title" class="form-label">Title</label>
                <input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($challenge['title']); ?>" required>
            </div>
            <div class="mb-3">
                <label for="description" class="form-label">Description</label>
                <textarea class="form-control" id="description" name="description" rows="5" required><?php echo htmlspecialchars($challenge['description']); ?></textarea>
            </div>
            <div class="mb-3">
                <label for="difficulty" class="form-label">Difficulty</label>
                <select class="form-select" id="difficulty" name="difficulty" required>
                    <option value="Easy" <?php echo ($challenge['difficulty'] === 'Easy') ? 'selected' : ''; ?>>Easy</option>
                    <option value="Medium" <?php echo ($challenge['difficulty'] === 'Medium') ? 'selected' : ''; ?>>Medium</option>
                    <option value="Hard" <?php echo ($challenge['difficulty'] === 'Hard') ? 'selected' : ''; ?>>Hard</option>
                </select>
            </div>
            <div class="mb-3">
                <label for="learning_style" class="form-label">Learning Style</label>
                <input type="text" class="form-control" id="learning_style" name="learning_style" value="<?php echo htmlspecialchars($challenge['learning_style']); ?>">
            </div>
            <div class="mb-3">
                <label for="expected_output" class="form-label">Expected Output</label>
                <textarea class="form-control" id="expected_output" name="expected_output" rows="3"><?php echo htmlspecialchars($challenge['expected_output']); ?></textarea>
            </div>
            <div class="mb-3">
                <label for="sample_cases_json" class="form-label">Sample Cases (JSON)</label>
                <textarea class="form-control" id="sample_cases_json" name="sample_cases_json" rows="5"><?php echo htmlspecialchars($challenge['sample_cases_json']); ?></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Update Challenge</button>
        </form>
    </div>
</body>
</html>
