"""Personal CRM — built with zuzu.

Demonstrates: Accent theming, sidebar nav, tab icons, card/kanban layouts,
relations, computed fields, Choice colors, Currency, group_by, required fields.
"""

import zuzu

# ── Records ──────────────────────────────────────────────────────────────

clients = zuzu.record("Clients",
    name="",
    email="",
    company="",
    status=zuzu.Choice(["lead", "active", "churned"], colors={
        "lead": "yellow",
        "active": "green",
        "churned": "red",
    }),
    revenue=zuzu.Currency(0.0, symbol="$"),
    notes="",
).require("name", "email")

deals = zuzu.record("Deals",
    title="",
    client=clients,
    value=zuzu.Currency(0.0, symbol="$"),
    stage=zuzu.Choice(["prospect", "proposal", "negotiation", "won", "lost"], colors={
        "prospect": "blue",
        "proposal": "purple",
        "negotiation": "yellow",
        "won": "green",
        "lost": "red",
    }),
    # Computed: win probability based on stage
    probability=lambda r: {"prospect": 10, "proposal": 30, "negotiation": 60, "won": 100, "lost": 0}.get(r.get("stage", ""), 0),
).require("title")

activities = zuzu.record("Activities",
    client=clients,
    type=zuzu.Choice(["call", "email", "meeting", "note"], colors={
        "call": "blue",
        "email": "purple",
        "meeting": "green",
        "note": "yellow",
    }),
    summary="",
    date=zuzu.date,
).require("summary")

# ── App — accent blue, sidebar nav, custom icon ─────────────────────────

app = zuzu.app("My CRM", records=[clients, deals, activities],
    accent="blue",
    nav="sidebar",
    icon="💼",
)

# Customize list pages with icons and layouts
app.page("Clients").show(title="name", subtitle="company", badge="status", sort="revenue", icon="👥", layout="cards")
app.page("Deals").show(title="title", subtitle="value", badge="stage", group_by="stage", icon="🤝", layout="kanban")
app.page("Activities").show(title="summary", subtitle="date", badge="type", icon="📋")

# Dashboard
with zuzu.page("Dashboard"):
    active_count = clients.where(clients.status == "active").count()
    pipeline = deals.where(deals.stage.is_in(["proposal", "negotiation"])).total("value")

    zuzu.metric("Total Clients", clients.count())
    zuzu.metric("Active", active_count)
    zuzu.metric("Pipeline", pipeline)
    zuzu.metric("Deals Won", deals.where(deals.stage == "won").count())

    zuzu.chart(deals, x="stage", y="value", aggregate="sum", kind="bar")

    zuzu.section("Recent Activity")
    zuzu.list(activities.sort("date", descending=True).first(5),
        title="summary",
        subtitle="date",
        badge="type",
        visible=activities.count() > 0,
    )

app.run()
