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

// Handle delete
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);

    // Delete completions first (foreign key safety)
    $pdo->prepare("DELETE FROM habit_completions WHERE habit_id = ?")->execute([$id]);

    // Delete habit
    $pdo->prepare("DELETE FROM habits WHERE id = ?")->execute([$id]);

    header("Location: habit_list.php?deleted=1");
    exit;
}

// Fetch all habits
$habits = $pdo->query("
    SELECT *
    FROM habits
    ORDER BY created_at DESC
")->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Habit List</title>
    <link rel="stylesheet" href="../assets/css/style.css">

    <style>
        .list-container {
            width: 900px;
            margin: 0 auto;
            color: #eee;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: #1a1a1f;
            border: 1px solid #444;
        }

        th, td {
            padding: 10px;
            border-bottom: 1px solid #333;
        }

        th {
            background: #2a2a30;
            color: #ffcc66;
        }

        tr:hover {
            background: #222;
        }

        .btn-edit {
            background: #4a90e2;
            padding: 6px 10px;
            border-radius: 4px;
            color: white;
            text-decoration: none;
        }

        .btn-delete {
            background: #b30000;
            padding: 6px 10px;
            border-radius: 4px;
            color: white;
            text-decoration: none;
        }

        .success {
            background: #2d662d;
            padding: 10px;
            border-radius: 4px;
            color: #c8ffc8;
            margin-bottom: 15px;
        }
    </style>
</head>
<body>

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

<div class="list-container">

    <h1 style="color:#ffcc66;">Habit List</h1>

    <?php if (isset($_GET['deleted'])): ?>
        <div class="success">Habit deleted successfully.</div>
    <?php endif; ?>

    <table>
        <tr>
            <th>Title</th>
            <th>Frequency</th>
            <th>Bounty</th>
            <th>Created</th>
            <th>Actions</th>
        </tr>

        <?php foreach ($habits as $habit): ?>
            <tr>
                <td><?= htmlspecialchars($habit['title']) ?></td>
                <td><?= ucfirst($habit['frequency']) ?></td>
                <td><?= $habit['is_bounty'] ? "Yes" : "No" ?></td>
                <td><?= $habit['created_at'] ?></td>
                <td>
                    <a class="btn-edit" href="habit_edit.php?id=<?= $habit['id'] ?>">Edit</a>
                    <a class="btn-delete"
                       onclick="return confirm('Delete this habit? This will remove all completions too.')"
                       href="habit_list.php?delete=<?= $habit['id'] ?>">
                       Delete
                    </a>
                </td>
            </tr>
        <?php endforeach; ?>

    </table>

</div>

</body>
</html>