
<?php
require_once 'header.php';

$db = getDB();

$year = intval($_GET['year'] ?? date('Y'));
$month = intval($_GET['month'] ?? date('n'));

$firstDay = date('w', strtotime("$year-$month-1"));
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// Get completions for this month
$startDate = "$year-" . str_pad($month, 2, '0', STR_PAD_LEFT) . "-01";
$endDate = "$year-" . str_pad($month, 2, '0', STR_PAD_LEFT) . "-$daysInMonth";

$stmt = $db->prepare("SELECT completed_date, COUNT(*) as count FROM completions WHERE completed_date BETWEEN ? AND ? GROUP BY completed_date");
$stmt->execute([$startDate, $endDate]);
$completions = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

$days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
?>

<h2 class="text-center mb-20" style="color: var(--gold);">📅 Quest Log</h2>

<div style="text-align: center; margin-bottom: 20px;">
    <a href="?year=<?php echo $month == 1 ? $year - 1 : $year; ?>&month=<?php echo $month == 1 ? 12 : $month - 1; ?>" class="nav-btn">&lt; Prev</a>
    <span style="margin: 0 20px; font-size: 1.3rem; color: var(--gold);"><?php echo date('F Y', strtotime("$year-$month-1")); ?></span>
    <a href="?year=<?php echo $month == 12 ? $year + 1 : $year; ?>&month=<?php echo $month == 12 ? 1 : $month + 1; ?>" class="nav-btn">Next &gt;</a>
</div>

<div style="background: rgba(0,0,0,0.8); padding: 20px; border-radius: 10px; border: 2px solid var(--gold);">
    <div class="calendar-grid">
        <?php foreach ($days as $day): ?>
            <div class="calendar-header"><?php echo $day; ?></div>
        <?php endforeach; ?>
        
        <?php for ($i = 0; $i < $firstDay; $i++): ?>
            <div class="calendar-day" style="opacity: 0.3;"></div>
        <?php endfor; ?>
        
        <?php for ($day = 1; $day <= $daysInMonth; $day++): 
            $date = "$year-" . str_pad($month, 2, '0', STR_PAD_LEFT) . "-" . str_pad($day, 2, '0', STR_PAD_LEFT);
            $dayOfWeek = date('w', strtotime($date));
            $isToday = $date === date('Y-m-d');
            $hasCompletion = isset($completions[$date]) && $completions[$date] > 0;
            
            // Get champion for this day
            $stmt = $db->prepare("SELECT * FROM champions WHERE day_index = ?");
            $stmt->execute([$dayOfWeek]);
            $dayChampion = $stmt->fetch();
        ?>
            <div class="calendar-day <?php echo $isToday ? 'today' : ''; ?> <?php echo $hasCompletion ? 'completed' : ''; ?>" 
                 onclick="showDayDetails(<?php echo $year; ?>, <?php echo $month; ?>, <?php echo $day; ?>)">
                <div class="day-number"><?php echo $day; ?></div>
                <img src="<?php echo getChampionImage($dayChampion['name'], 'card'); ?>" 
                     alt="<?php echo $dayChampion['name']; ?>"
                     class="day-champion"
                     onerror="this.style.display='none'">
            </div>
        <?php endfor; ?>
    </div>
</div>

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

