<?php
session_start();

$admin_password = 'password123'; // For now, using a hardcoded password

if (isset($_POST['password'])) {
    if ($_POST['password'] === $admin_password) {
        $_SESSION['admin_logged_in'] = true;
    }
}

include 'includes/header.php';

if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true):
    require_once 'db/config.php';
    $pdo = db();
    $stmt = $pdo->query("SELECT * FROM quiz_attempts ORDER BY created_at DESC LIMIT 20");
    $attempts = $stmt->fetchAll();

    $total_attempts_stmt = $pdo->query("SELECT COUNT(*) as total FROM quiz_attempts");
    $total_attempts = $total_attempts_stmt->fetch()['total'];

    $style_distribution_stmt = $pdo->query("SELECT primary_style, COUNT(*) as count FROM quiz_attempts GROUP BY primary_style");
    $style_distribution = $style_distribution_stmt->fetchAll(PDO::FETCH_KEY_PAIR);

    $styles_info = [
        'v' => ['name' => 'Visual'],
        'a' => ['name' => 'Auditory'],
        'r' => ['name' => 'Read/Write'],
        'k' => ['name' => 'Kinesthetic'],
    ];

?>

<div class="container mt-5">
    <h2 class="text-center mb-4">Admin Dashboard</h2>

    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Total Attempts</h5>
                    <p class="card-text display-4"><?php echo $total_attempts; ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Primary Style Distribution</h5>
                    <?php
                    $total_styles = array_sum($style_distribution);
                    foreach ($styles_info as $key => $style):
                        $count = $style_distribution[$key] ?? 0;
                        $percentage = $total_styles > 0 ? ($count / $total_styles) * 100 : 0;
                    ?>
                    <div class="mb-2">
                        <span><?php echo $style['name']; ?></span>
                        <div class="progress" style="height: 20px;">
                            <div class="progress-bar" role="progressbar" style="width: <?php echo $percentage; ?>%;" aria-valuenow="<?php echo $percentage; ?>" aria-valuemin="0" aria-valuemax="100"><?php echo round($percentage); ?>%</div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
    
    <h4 class="mt-5">Last 20 Quiz Attempts</h4>
    <a href="download.php" class="btn btn-success mb-3">Download Full Attempts CSV</a>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>Visual</th>
                <th>Auditory</th>
                <th>Read/Write</th>
                <th>Kinesthetic</th>
                <th>Primary Style</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($attempts as $attempt): ?>
                <tr>
                    <td><?php echo $attempt['id']; ?></td>
                    <td><?php echo $attempt['v_score']; ?></td>
                    <td><?php echo $attempt['a_score']; ?></td>
                    <td><?php echo $attempt['r_score']; ?></td>
                    <td><?php echo $attempt['k_score']; ?></td>
                    <td><?php echo $attempt['primary_style']; ?></td>
                    <td><?php echo $attempt['created_at']; ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php else: ?>

<link rel="stylesheet" href="assets/css/admin.css">

<div class="login-container">
    <h2 class="text-center mb-4">Admin Login</h2>
    <form action="admin.php" method="post">
        <div class="mb-3">
            <label for="password" class="form-label">Password</label>
            <input type="password" class="form-control" id="password" name="password" required>
        </div>
        <button type="submit" class="btn btn-primary w-100">Login</button>
    </form>
</div>

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