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

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

// First day of month
$firstDay = strtotime("$year-$month-01");
$daysInMonth = date('t', $firstDay);
$startWeekday = date('w', $firstDay); // 0 = Sunday

// Fetch all completions for this month
$stmt = $pdo->prepare("
    SELECT 
        hc.completion_date,
        c.name AS champion_name,
        h.title AS habit_title,
        h.is_bounty
    FROM habit_completions hc
    JOIN champions c ON hc.champion_id = c.id
    JOIN habits h ON hc.habit_id = h.id
    WHERE MONTH(hc.completion_date) = ? AND YEAR(hc.completion_date) = ?
    ORDER BY hc.completion_date ASC
");
$stmt->execute([$month, $year]);
$completions = $stmt->fetchAll();

// Group completions by date
$byDate = [];
foreach ($completions as $row) {
    $byDate[$row['completion_date']][] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Calendar</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <style>
        .calendar-container {
            width: 900px;
            margin: 0 auto;
            color: #eee;
        }

        .calendar-header {
            text-align: center;
            margin-bottom: 20px;
        }

        .calendar-grid {
            display: grid;
            grid-template-columns: repeat(7, 1fr);
            gap: 5px;
        }

        .calendar-cell {
            background: #1a1a1f;
            border: 1px solid #444;
            min-height: 120px;
            padding: 5px;
            position: relative;
        }

        .calendar-cell h4 {
            margin: 0;
            font-size: 14px;
            color: #ffcc66;
        }

        .entry {
            background: #333;
            padding: 3px 5px;
            margin-top: 4px;
            border-radius: 4px;
            font-size: 12px;
        }

        .bounty {
            background: #7a0000;
        }

        .nav-buttons {
            text-align: center;
            margin-bottom: 20px;
        }

        .nav-buttons a {
            color: #ffcc66;
            margin: 0 10px;
            text-decoration: none;
        }
    </style>
</head>
<body>

<div class="calendar-container">

    <div class="calendar-header">
        <h1><?= date('F Y', $firstDay) ?></h1>
    </div>

    <div class="nav-buttons">
        <?php
        $prevMonth = $month - 1;
        $prevYear = $year;
        if ($prevMonth < 1) { $prevMonth = 12; $prevYear--; }

        $nextMonth = $month + 1;
        $nextYear = $year;
        if ($nextMonth > 12) { $nextMonth = 1; $nextYear++; }
        ?>
        <a href="?month=<?= $prevMonth ?>&year=<?= $prevYear ?>">← Previous</a>
        <a href="?month=<?= $nextMonth ?>&year=<?= $nextYear ?>">Next →</a>
    </div>

    <div class="calendar-grid">

        <!-- Weekday labels -->
        <?php
        $weekdays = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
        foreach ($weekdays as $wd) {
            echo "<div style='text-align:center; font-weight:bold;'>$wd</div>";
        }
        ?>

        <!-- Empty cells before month starts -->
        <?php for ($i = 0; $i < $startWeekday; $i++): ?>
            <div class="calendar-cell"></div>
        <?php endfor; ?>

        <!-- Actual days -->
        <?php for ($day = 1; $day <= $daysInMonth; $day++): ?>
            <?php
            $dateStr = "$year-$month-" . str_pad($day, 2, "0", STR_PAD_LEFT);
            ?>
            <div class="calendar-cell">
                <h4><?= $day ?></h4>

                <?php if (isset($byDate[$dateStr])): ?>
                    <?php foreach ($byDate[$dateStr] as $entry): ?>
                        <div class="entry <?= $entry['is_bounty'] ? 'bounty' : '' ?>">
                            <strong><?= $entry['champion_name'] ?>:</strong>
                            <?= htmlspecialchars($entry['habit_title']) ?>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        <?php endfor; ?>

    </div>

</div>

</body>
</html>