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

$todaysChampion = getTodaysChampion($pdo);

// Fetch today's habits
$today = date('Y-m-d');
$dow = date('w');

// Daily habits
$sql = "
    SELECT * FROM habits
    WHERE frequency = 'daily'
       OR (frequency = 'weekly' AND WEEKDAY(CURDATE()) = WEEKDAY(CURDATE()))
       OR (frequency = 'fortnightly' AND (WEEK(CURDATE()) % 2) = (WEEK(created_at) % 2))
       OR (frequency = 'monthly' AND DAY(CURDATE()) = DAY(created_at))
       OR (frequency = 'one_off' AND specific_date = CURDATE())
";
$habits = $pdo->query($sql)->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Dashboard</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <script src="../assets/js/app.js"></script>
</head>
<body>

<div class="dashboard-container">

    <h1 class="champion-title">
        Today’s Champion:
        <span class="champion-name"><?= $todaysChampion['name'] ?></span>
        <span class="champion-class">(<?= $todaysChampion['class_name'] ?>)</span>
    </h1>

    <h2>Today's Habits & Bounties</h2>

    <div class="habit-list">
        <?php if (empty($habits)): ?>
            <p>No habits for today.</p>
        <?php else: ?>
            <?php foreach ($habits as $habit): ?>
                <div class="habit-card" id="habit-<?= $habit['id'] ?>">
                    <div class="habit-info">
                        <h3><?= htmlspecialchars($habit['title']) ?></h3>
                        <p><?= htmlspecialchars($habit['description']) ?></p>

                        <?php if ($habit['is_bounty']): ?>
                            <span class="bounty-tag">BOUNTY</span>
                        <?php endif; ?>
                    </div>

                    <button class="complete-btn"
                        onclick="completeHabit(<?= $habit['id'] ?>)">
                        Complete
                    </button>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

</div>

</body>
</html>