<?php
require_once 'header.php';

// Get stats
$db = getDB();
$dayOfWeek = date('w');

// Get habits for today
$stmt = $db->query("SELECT * FROM habits ORDER BY FIELD(time_of_day, 'morning', 'midday', 'evening')");
$habits = $stmt->fetchAll();

// Get bounties
$stmt = $db->query("SELECT * FROM bounties WHERE completed = FALSE ORDER BY created_at DESC");
$bounties = $stmt->fetchAll();

// Get champion progress
$stmt = $db->prepare("SELECT xp, level FROM champion_progress WHERE champion_id = ?");
$stmt->execute([$currentChampion['id']]);
$progress = $stmt->fetch();

$xp = $progress['xp'] ?? 0;
$level = $progress['level'] ?? 1;
$maxXP = $level * 100;
$xpPercent = ($xp / $maxXP) * 100;

// Get stats
$stmt = $db->query("SELECT COUNT(*) as total FROM habits");
$totalQuests = $stmt->fetch()['total'];

$stmt = $db->prepare("SELECT COUNT(*) as completed FROM completions WHERE completed_date = CURDATE()");
$stmt->execute();
$completedToday = $stmt->fetch()['completed'];

$stmt = $db->query("SELECT SUM(quantity) as total FROM items");
$totalItems = $stmt->fetch()['total'] ?? 0;

// Calculate streak
$streak = 0;
for ($i = 0; $i < 365; $i++) {
    $date = date('Y-m-d', strtotime("-$i days"));
    $stmt = $db->prepare("SELECT COUNT(*) as count FROM completions WHERE completed_date = ?");
    $stmt->execute([$date]);
    if ($stmt->fetch()['count'] > 0) {
        $streak++;
    } else if ($i > 0) {
        break;
    }
}
?>

<div class="hero-section">
    <div class="champion-portrait">
        <img src="<?php echo getChampionImage($currentChampion['name'], 'flyer'); ?>" 
             alt="<?php echo $currentChampion['name']; ?>"
             onerror="this.src='data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22><?php echo $currentChampion['emoji']; ?></text></svg>'">
    </div>
    <h1 class="champion-name"><?php echo $currentChampion['name']; ?></h1>
    <p class="champion-title"><?php echo $currentChampion['title']; ?> • <?php echo $currentChampion['role']; ?></p>
    <p class="champion-tagline">"<?php echo $currentChampion['tagline']; ?>"</p>
    
    <div class="xp-bar-container" style="margin-top: 20px;">
        <div class="xp-bar-fill" style="width: <?php echo $xpPercent; ?>%;">
            Level <?php echo $level; ?> - <?php echo $xp; ?> / <?php echo $maxXP; ?> XP
        </div>
    </div>
</div>

<div class="stats-bar">
    <div class="stat-box">
        <div class="stat-label">Total Quests</div>
        <div class="stat-value"><?php echo $totalQuests; ?></div>
    </div>
    <div class="stat-box">
        <div class="stat-label">Completed</div>
        <div class="stat-value"><?php echo $completedToday; ?></div>
    </div>
    <div class="stat-box">
        <div class="stat-label">Streak</div>
        <div class="stat-value"><?php echo $streak; ?></div>
    </div>
    <div class="stat-box">
        <div class="stat-label">Items</div>
        <div class="stat-value"><?php echo $totalItems; ?></div>
    </div>
</div>

<div class="quest-grid">
    <?php foreach (['morning', 'midday', 'evening'] as $time): ?>
    <div class="quest-category">
        <div class="category-header">
            <span class="category-icon">
                <?php echo $time === 'morning' ? '🌅' : ($time === 'midday' ? '☀️' : '🌙'); ?>
            </span>
            <span class="category-title"><?php echo ucfirst($time); ?> Quests</span>
        </div>
        <div class="quest-list">
            <?php 
            $timeHabits = array_filter($habits, function($h) use ($time) { return $h['time_of_day'] === $time; });
            if (empty($timeHabits)): 
            ?>
                <p style="color: #666; text-align: center; padding: 20px;">No quests available</p>
            <?php else: 
                foreach ($timeHabits as $habit): 
                    $stmt = $db->prepare("SELECT id FROM completions WHERE habit_id = ? AND completed_date = CURDATE()");
                    $stmt->execute([$habit['id']]);
                    $isCompleted = $stmt->fetch();
            ?>
                <div class="quest-item <?php echo $isCompleted ? 'quest-complete' : ''; ?>" onclick="completeHabit(<?php echo $habit['id']; ?>)">
                    <div class="quest-info">
                        <?php if ($habit['image_path']): ?>
                            <img src="<?php echo $habit['image_path']; ?>" alt="" class="quest-img">
                        <?php endif; ?>
                        <div>
                            <div class="quest-name"><?php echo $habit['name']; ?></div>
                            <div class="quest-desc"><?php echo $habit['description']; ?></div>
                        </div>
                    </div>
                    <span class="quest-xp"><?php echo $isCompleted ? '✓' : '+' . $habit['xp_reward'] . ' XP'; ?></span>
                </div>
            <?php endforeach; endif; ?>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<div class="quest-category mt-20">
    <div class="category-header">
        <span class="category-icon">💰</span>
        <span class="category-title">Active Bounties</span>
    </div>
    <div class="quest-list">
        <?php if (empty($bounties)): ?>
            <p style="color: #666; text-align: center;">No active bounties</p>
        <?php else: ?>
            <?php foreach ($bounties as $bounty): ?>
                <div class="quest-item" onclick="completeBounty(<?php echo $bounty['id']; ?>)">
                    <div class="quest-info">
                        <?php if ($bounty['image_path']): ?>
                            <img src="<?php echo $bounty['image_path']; ?>" alt="" class="quest-img">
                        <?php endif; ?>
                        <div>
                            <div class="quest-name"><?php echo $bounty['name']; ?></div>
                            <div class="quest-desc"><?php echo $bounty['description']; ?></div>
                        </div>
                    </div>
                    <span class="quest-xp">+<?php echo $bounty['xp_reward']; ?> XP</span>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

<?php require_once 'footer.php'; ?>
