ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/dashboard_error.log');




<?php
require_once "../includes/config.php";
require_once "../includes/functions.php";

// ------------------------------------------------------------
// GET TODAY'S CHAMPION
// ------------------------------------------------------------
$todaysChampion = getTodaysChampion($pdo);
$championId = $todaysChampion['id'];

// ------------------------------------------------------------
// GET CHAMPION STATS
// ------------------------------------------------------------
$statStmt = $pdo->prepare("SELECT total_xp, level FROM champion_stats WHERE champion_id = ?");
$statStmt->execute([$championId]);
$stats = $statStmt->fetch();

$totalXP = $stats ? $stats['total_xp'] : 0;
$level   = $stats ? $stats['level'] : 1;

// ------------------------------------------------------------
// GET TODAY'S HABITS
// ------------------------------------------------------------
$habits = getHabitsForToday($pdo);

// ------------------------------------------------------------
// GET LATEST ITEMS
// ------------------------------------------------------------
$latestItems = $pdo->query("
    SELECT id, title, image_path, rarity, quantity, quantity_max
    FROM items
    ORDER BY id DESC
    LIMIT 5
")->fetchAll();

// ------------------------------------------------------------
// GET LOW STOCK ITEMS
// ------------------------------------------------------------
$lowStockItems = $pdo->query("
    SELECT id, title, image_path, quantity, quantity_max
    FROM items
    WHERE 
        (quantity IS NOT NULL AND quantity <= 5)
        OR 
        (quantity_max IS NOT NULL AND quantity_max > 0 AND (quantity / quantity_max) <= 0.20)
    ORDER BY quantity ASC
    LIMIT 5
")->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Dashboard</title>
    <link rel="stylesheet" href="../assets/css/style.css">

    <style>
        body {
            background: #111;
            color: #eee;
            font-family: Arial, sans-serif;
        }

        .section {
            background: #1a1a1f;
            padding: 15px;
            margin: 20px auto;
            width: 90%;
            border-radius: 8px;
            border: 1px solid #333;
        }

        .section h2 {
            margin-top: 0;
            color: #ffcc66;
        }

        .item-box, .habit-box {
            display: flex;
            align-items: center;
            background: #222;
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 10px;
            border: 1px solid #333;
        }

        .item-box img, .habit-box img {
            width: 60px;
            height: 60px;
            border-radius: 6px;
            object-fit: cover;
            margin-right: 10px;
            border: 1px solid #444;
        }

        .xp-bar {
            background: #333;
            height: 20px;
            border-radius: 10px;
            overflow: hidden;
            margin-top: 5px;
        }

        .xp-fill {
            background: #ffcc66;
            height: 100%;
        }
    </style>
</head>
<body>

<?php include "../includes/menu.php"; ?>

<div class="section">
    <h2>Today's Champion</h2>
    <p><strong><?= htmlspecialchars($todaysChampion['name']) ?></strong> (<?= htmlspecialchars($todaysChampion['class_name']) ?>)</p>

    <p>Level: <?= $level ?></p>
    <p>XP: <?= $totalXP ?> / <?= getXPForNextLevel($level) ?></p>

    <div class="xp-bar">
        <div class="xp-fill" style="width: <?= ($totalXP / getXPForNextLevel($level)) * 100 ?>%;"></div>
    </div>
</div>

<div class="section">
    <h2>Today's Habits</h2>

    <?php if (empty($habits)): ?>
        <p>No habits scheduled for today.</p>
    <?php else: ?>
        <?php foreach ($habits as $habit): ?>
            <div class="habit-box">
                <?php if ($habit['image_path']): ?>
                    <img src="../<?= $habit['image_path'] ?>">
                <?php else: ?>
                    <img src="../assets/images/icons/scroll.svg">
                <?php endif; ?>

                <div>
                    <strong><?= htmlspecialchars($habit['title']) ?></strong><br>
                    XP: <?= $habit['xp_reward'] ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<div class="section">
    <h2>Latest Items</h2>

    <?php if (empty($latestItems)): ?>
        <p>No items found.</p>
    <?php else: ?>
        <?php foreach ($latestItems as $item): ?>
            <div class="item-box">
                <?php if ($item['image_path']): ?>
                    <img src="../<?= $item['image_path'] ?>">
                <?php else: ?>
                    <img src="../assets/images/icons/bag.svg">
                <?php endif; ?>

                <div>
                    <strong><?= htmlspecialchars($item['title']) ?></strong><br>
                    Qty: <?= $item['quantity'] ?? 0 ?> / <?= $item['quantity_max'] ?? '-' ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<div class="section">
    <h2>Low Stock Items</h2>

    <?php if (empty($lowStockItems)): ?>
        <p>No low-stock items.</p>
    <?php else: ?>
        <?php foreach ($lowStockItems as $item): ?>
            <div class="item-box">
                <?php if ($item['image_path']): ?>
                    <img src="../<?= $item['image_path'] ?>">
                <?php else: ?>
                    <img src="../assets/images/icons/bag.svg">
                <?php endif; ?>

                <div>
                    <strong><?= htmlspecialchars($item['title']) ?></strong><br>
                    Qty: <?= $item['quantity'] ?? 0 ?> / <?= $item['quantity_max'] ?? '-' ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

</body>
</html>