"""Habit Tracker — built with zuzu.

Demonstrates: Relations, Choice colors, computed fields, visibility, group_by.
"""

import zuzu

habits = zuzu.record("Habits",
    name="",
    emoji="",
    frequency=zuzu.Choice(["daily", "weekdays", "weekly"], colors={
        "daily": "green",
        "weekdays": "blue",
        "weekly": "purple",
    }),
    active=True,
    # Computed: display name with emoji
    display=lambda r: f"{r.get('emoji', '')} {r.get('name', '')}".strip(),
)

logs = zuzu.record("Habit Logs",
    habit=habits,
    date=zuzu.date,
    done=True,
    note="",
)

app = zuzu.app("Habit Tracker", records=[habits, logs])

app.page("Habits").show(title="name", subtitle="frequency", badge="emoji")
app.page("Habit Logs").show(title="note", subtitle="date", badge="done")

with zuzu.page("Today"):
    active = habits.where(habits.active == True)
    total_active = active.count()
    total_logs = logs.count()

    zuzu.metric("Active Habits", total_active)
    zuzu.metric("Total Logged", total_logs)

    zuzu.section("Active Habits")
    zuzu.list(active, title="name", subtitle="frequency", badge="emoji",
        empty="No active habits. Add one to get started!")

    zuzu.section("Recent Log")
    zuzu.list(logs.sort("date", descending=True).first(10),
        title="note",
        subtitle="date",
        visible=total_logs > 0,
    )

app.run()
