root = Card([periodSelector, kpiRow, chartsRow, topCustomersSection, atRiskSection, failedPaymentsSection, seatUtilizationSection, inviteAdminSection], "card", "column", "l")

periodSelector = Card([periodLabel, periodSelect], "clear", "row", "m", "center", "between")
periodLabel = TextContent("Reporting period:", "small-heavy")
periodSelect = Select("reporting_period", periodOptions, "Select period", null, "small")
periodOptions = [opt30d, opt90d, opt180d, opt365d, optYtd, optCustom]
opt30d = SelectItem("30d", "Last 30 days")
opt90d = SelectItem("90d", "Last 90 days")
opt180d = SelectItem("180d", "Last 180 days")
opt365d = SelectItem("365d", "Last 365 days")
optYtd = SelectItem("ytd", "Year to date")
optCustom = SelectItem("custom", "Custom range")

kpiRow = Card([mrrCard, arrCard, churnCard, npsCard], "clear", "row", "m", "stretch", "start", true)
mrrCard = Card([mrrLabel, mrrValue, mrrTrend], "card", "column", "s")
mrrLabel = TextContent("Monthly Recurring Revenue", "small")
mrrValue = TextContent("$247,892", "large-heavy")
mrrTrend = TextContent("↑ 12.3% vs last month", "small")
arrCard = Card([arrLabel, arrValue, arrTrend], "card", "column", "s")
arrLabel = TextContent("Annual Recurring Revenue", "small")
arrValue = TextContent("$2,974,704", "large-heavy")
arrTrend = TextContent("↑ 18.7% vs last year", "small")
churnCard = Card([churnLabel, churnValue, churnTrend], "card", "column", "s")
churnLabel = TextContent("Churn this month", "small")
churnValue = TextContent("2.1%", "large-heavy")
churnTrend = TextContent("↓ 0.4pp vs last month", "small")
npsCard = Card([npsLabel, npsValue, npsTrend], "card", "column", "s")
npsLabel = TextContent("Current NPS", "small")
npsValue = TextContent("67", "large-heavy")
npsTrend = TextContent("↑ 5 pts vs last quarter", "small")

chartsRow = Card([signupsChartCard, planSplitCard, featureAdoptionCard], "clear", "row", "m", "stretch", "start", true)
signupsChartCard = Card([signupsHeader, signupsChart], "card", "column", "m")
signupsHeader = CardHeader("New signups (90 days)", "Daily signups with 7-day moving average")
signupsChart = LineChart(signupsLabels, [signupsSeries, signupsMaSeries], "natural", "Date", "Signups")
signupsLabels = ["Day 1", "Day 8", "Day 15", "Day 22", "Day 29", "Day 36", "Day 43", "Day 50", "Day 57", "Day 64", "Day 71", "Day 78", "Day 85", "Day 90"]
signupsSeries = Series("Daily", [12, 18, 15, 22, 19, 25, 28, 24, 30, 27, 33, 31, 35, 38])
signupsMaSeries = Series("7-day MA", [16, 17, 18, 20, 21, 23, 25, 26, 27, 28, 30, 31, 33, 34])
planSplitCard = Card([planSplitHeader, planSplitChart], "card", "column", "m")
planSplitHeader = CardHeader("Customers by plan", "Distribution across pricing tiers")
planSplitChart = PieChart(planLabels, planValues, "donut")
planLabels = ["Starter", "Professional", "Enterprise", "Custom"]
planValues = [1240, 567, 189, 34]
featureAdoptionCard = Card([featureHeader, featureChart], "card", "column", "m")
featureHeader = CardHeader("New feature adoption", "Last 30 days • % of active users")
featureChart = HorizontalBarChart(featureLabels, [featureSeries], "grouped", "Adoption %", "Feature")
featureLabels = ["AI Insights", "Workflow Builder", "Advanced Analytics"]
featureSeries = Series("Adoption", [34, 28, 19])

topCustomersSection = Card([topCustomersHeader, topCustomersTable], "card", "column", "m")
topCustomersHeader = CardHeader("Top 10 customers by spend", "Seats and monthly spend")
topCustomersTable = Table([custNameCol, custPlanCol, custSeatsCol, custSpendCol, custActionCol])
custNameCol = Col("Customer", ["Acme Corp", "Globex Inc", "Wayne Enterprises", "Stark Industries", "Umbrella Corp", "Tyrell Corp", "Weyland-Yutani", "Cyberdyne Systems", "Massive Dynamic", "Initech"])
custPlanCol = Col("Plan", ["Enterprise", "Enterprise", "Custom", "Enterprise", "Professional", "Enterprise", "Custom", "Enterprise", "Professional", "Enterprise"])
custSeatsCol = Col("Seats", [450, 380, 320, 290, 275, 260, 245, 230, 215, 200], "number")
custSpendCol = Col("Monthly spend", ["$18,450", "$15,200", "$14,800", "$13,050", "$11,000", "$10,400", "$9,800", "$9,200", "$8,600", "$8,000"])
custActionCol = Col("Action", ["View", "View", "View", "View", "View", "View", "View", "View", "View", "View"], "action")

atRiskSection = Card([atRiskHeader, atRiskTable], "card", "column", "m")
atRiskHeader = CardHeader("Accounts at risk", "Customers with health score < 40 or recent negative signals")
atRiskTable = Table([riskNameCol, riskHealthCol, riskReasonCol, riskActionCol])
riskNameCol = Col("Customer", ["DataFlow Labs", "Nexus Technologies", "Apex Solutions", "Meridian Systems", "Quantum Analytics"])
riskHealthCol = Col("Health score", [32, 28, 35, 38, 25], "number")
riskReasonCol = Col("Primary reason", ["Low feature adoption (12%)", "No login in 21 days", "Support tickets ↑ 300%", "Champion left company", "Payment failed 2x"])
riskActionCol = Col("Action", ["Engage", "Engage", "Engage", "Engage", "Engage"], "action")

failedPaymentsSection = Card([failedHeader, failedTable], "card", "column", "m")
failedHeader = CardHeader("Failed payments (last 7 days)", "Retry or contact customer")
failedTable = Table([failCustomerCol, failAmountCol, failDateCol, failReasonCol, failRetryCol])
failCustomerCol = Col("Customer", ["BrightPath Co", "Vertex Group", "Horizon Labs", "Summit Digital", "Pinnacle Systems", "Catalyst Inc", "Beacon Analytics"])
failAmountCol = Col("Amount", ["$2,450", "$1,200", "$890", "$3,100", "$550", "$1,800", "$720"])
failDateCol = Col("Failed on", ["2024-01-18", "2024-01-17", "2024-01-17", "2024-01-16", "2024-01-15", "2024-01-14", "2024-01-13"])
failReasonCol = Col("Reason", ["Card expired", "Insufficient funds", "Card declined", "Card expired", "Bank blocked", "Card expired", "Insufficient funds"])
failRetryCol = Col("Retry", ["Retry", "Retry", "Retry", "Retry", "Retry", "Retry", "Retry"], "action")

seatUtilizationSection = Card([seatHeader, seatUtilTable], "card", "column", "m")
seatHeader = CardHeader("Seat utilization", "Company-wide license usage by plan")
seatUtilTable = Table([seatPlanCol, seatTotalCol, seatUsedCol, seatUtilCol, seatTrendCol])
seatPlanCol = Col("Plan", ["Starter", "Professional", "Enterprise", "Custom", "Total"])
seatTotalCol = Col("Total seats", [12400, 11340, 9450, 2040, 35230], "number")
seatUsedCol = Col("Active seats", [8920, 9870, 8120, 1820, 28730], "number")
seatUtilCol = Col("Utilization", ["72%", "87%", "86%", "89%", "82%"])
seatTrendCol = Col("Trend (30d)", ["↑ 3%", "↑ 2%", "→ 0%", "↑ 1%", "↑ 2%"])

inviteAdminSection = Card([inviteHeader, inviteButton], "card", "column", "m")
inviteHeader = CardHeader("Invite another admin", "Grant admin access to a team member")
inviteButton = Buttons([inviteBtn], "row")
inviteBtn = Button("Invite admin", null, "primary")

inviteDialog = DialogBlock("Invite admin", "Invite new administrator", "Enter the email address of the person you want to invite as an admin. They will receive an invitation link valid for 7 days.", [inviteForm], "default")
inviteForm = Form("invite-admin", inviteFormButtons, [inviteEmailField])
inviteEmailField = FormControl("Email address", inviteEmailInput, "Must be a valid work email")
inviteEmailInput = Input("email", "admin@company.com", "email")
inviteFormButtons = Buttons([inviteSubmitBtn, inviteCancelBtn])
inviteSubmitBtn = Button("Send invitation", null, "primary")
inviteCancelBtn = Button("Cancel", null, "tertiary")