from puepy import Application, Page, t

try:
    from data import CHARTS, POLICIES, SUMMARY, TIMELINE
except Exception:
    from .data import CHARTS, POLICIES, SUMMARY, TIMELINE

app = Application()


def pct(value):
    return ("%.1f" % value) + "%"


class Dashboard(Page):
    def populate(self):
        with t.main(classes=["dashboard"]):
            self.hero()
            with t.div(classes=["shell"]):
                self.kpis()
                self.readout()
                with t.div(classes=["chart-grid"]):
                    self.chart_card(
                        "Policy Status",
                        CHARTS["Policy Status"],
                        "Two buckets. Most policy instruments are already active, which makes execution more important than announcements.",
                    )
                    self.chart_card(
                        "Jurisdiction",
                        CHARTS["Jurisdiction"],
                        "Federal and provincial policy are almost evenly split; the machinery is shared whether anyone likes it or not.",
                    )
                    self.chart_card(
                        "Policy Theme",
                        CHARTS["Policy Theme"],
                        "Carbon pricing remains the centre of gravity, with clean energy and transport close behind.",
                    )
                    self.chart_card(
                        "Technology Focus",
                        CHARTS["Technology Focus"],
                        "Carbon management dominates the stack, followed by transport, power systems, and sustainable finance.",
                    )
                    self.chart_card(
                        "Target Audience",
                        CHARTS["Target Audience"],
                        "The burden spreads across the whole market, but large emitters and real-economy firms carry the operational load.",
                    )
                    self.timeline_card()
                self.policy_table()

    def hero(self):
        with t.section(classes=["hero"]):
            with t.div(classes=["eyebrow"]):
                t.i(classes=["ri-line-chart-line"])
                t.span("Canada climate policy dashboard")
            t.h1("policy is already dense. the next problem is execution.")
            t.p(
                "Canada has no shortage of climate policy. The harder question is what the stack actually targets, who has to act, and where the burden sits."
            )

    def kpis(self):
        with t.section(classes=["kpi-grid"]):
            self.kpi(
                "Policies tracked",
                str(SUMMARY["policies"]),
                "federal and provincial instruments",
            )
            self.kpi("Active", pct(SUMMARY["active_share"]), "not just announced")
            self.kpi("Federal share", pct(SUMMARY["federal_share"]), "rest is provincial")
            self.kpi(
                "Top theme",
                SUMMARY["top_theme"],
                pct(SUMMARY["top_theme_share"]) + " of the policy set",
            )

    def kpi(self, label, value, note):
        with t.article(classes=["kpi"]):
            t.span(label, classes=["kpi-label"])
            t.strong(value, classes=["kpi-value"])
            t.span(note, classes=["kpi-note"])

    def readout(self):
        with t.section(classes=["readout"]):
            with t.div(classes=["readout-copy"]):
                t.h2("what the stack says")
                t.p(
                    "The policy set is active, carbon-price heavy, split between federal and provincial machinery, and aimed most directly at carbon management, transport, and large emitters. The policy question is no longer whether Canada has tools. It is whether those tools line up with execution."
                )
            with t.div(classes=["readout-list"]):
                self.readout_item("1", "carbon pricing is the centre")
                self.readout_item("2", "jurisdiction is shared")
                self.readout_item("3", "execution sits with firms")

    def readout_item(self, number, text):
        with t.div(classes=["readout-item"]):
            t.span(number)
            t.strong(text)

    def chart_card(self, title, values, note):
        with t.article(classes=["chart-card"]):
            t.h2(title)
            t.p(note, classes=["chart-note"])
            with t.div(classes=["bars"]):
                for label, value in values:
                    with t.div(classes=["bar-row"]):
                        with t.div(classes=["bar-meta"]):
                            t.span(label)
                            t.strong(pct(value))
                        with t.div(classes=["bar-track"]):
                            t.div("", classes=["bar-fill"], style="width: " + pct(value) + ";")

    def timeline_card(self):
        max_new = 7
        with t.article(classes=["chart-card", "timeline-card"]):
            t.h2("Policy Timeline")
            t.p(
                "The stack thickened fastest after 2018. The cumulative count matters more than any single year.",
                classes=["chart-note"],
            )
            with t.div(classes=["timeline"]):
                for year, added, cumulative in TIMELINE:
                    h = int(18 + (added / max_new) * 92)
                    with t.div(classes=["time-col"]):
                        t.span(str(cumulative), classes=["time-total"])
                        t.div("", classes=["time-bar"], style="height: " + str(h) + "px;")
                        t.span(str(year), classes=["time-year"])

    def policy_table(self):
        with t.section(classes=["table-card"]):
            with t.div(classes=["table-head"]):
                t.h2("Policy Details")
                t.p(
                    "The full policy list behind the dashboard, grouped by name, target audience, and plain-language description."
                )
            with t.div(classes=["table-wrap"]):
                with t.table():
                    with t.thead():
                        with t.tr():
                            t.th("Policy")
                            t.th("Audience")
                            t.th("Description")
                    with t.tbody():
                        for name, audience, description in POLICIES:
                            with t.tr():
                                t.td(name, classes=["policy-name"])
                                with t.td():
                                    t.span(audience, classes=["audience-tag"])
                                t.td(description)


@app.page()
class DefaultPage(Dashboard):
    pass


app.mount("#app")
