"""Reading List — built with zuzu.

Demonstrates: Rating type, Choice colors, computed fields, group_by, visibility.
"""

import zuzu

books = zuzu.record("Books",
    title="",
    author="",
    status=zuzu.Choice(["to-read", "reading", "finished", "abandoned"], colors={
        "to-read": "blue",
        "reading": "yellow",
        "finished": "green",
        "abandoned": "red",
    }),
    rating=zuzu.Rating(0, max=5),
    genre=zuzu.Choice(["fiction", "non-fiction", "technical", "biography", "other"], colors={
        "fiction": "purple",
        "non-fiction": "blue",
        "technical": "green",
        "biography": "yellow",
        "other": "orange",
    }),
    notes="",
    # Computed: display label
    label=lambda r: f"{'★' * r.get('rating', 0)}{'☆' * (5 - r.get('rating', 0))}" if r.get("rating", 0) > 0 else "",
)

app = zuzu.app("Reading List", records=[books])

app.page("Books").show(title="title", subtitle="author", badge="status", group_by="status")

with zuzu.page("Dashboard"):
    total = books.count()
    finished = books.where(books.status == "finished").count()
    reading = books.where(books.status == "reading").count()

    zuzu.metric("Total Books", total)
    zuzu.metric("Reading", reading)
    zuzu.metric("Finished", finished)
    zuzu.metric("Avg Rating", books.where(books.status == "finished").average("rating"), visible=finished > 0)

    zuzu.chart(books, x="genre", y="title", aggregate="count", kind="bar")

    zuzu.section("Currently Reading")
    zuzu.list(
        books.where(books.status == "reading"),
        title="title",
        subtitle="author",
        empty="Nothing in progress. Pick up a book!",
        visible=reading > 0,
    )

    zuzu.section("To Read Next")
    zuzu.list(
        books.where(books.status == "to-read").first(5),
        title="title",
        subtitle="author",
        badge="genre",
        visible=books.where(books.status == "to-read").count() > 0,
    )

app.run()
