import { ready, type VMState } from "../index.js"; import { leadQuelleLabelKey, leadStatusLabelKey, type LeadQuelle, type LeadStatus } from "./lead-list.vm.js"; /** * Lead statistics view-model (R-P7, P7 Lead-Inbox). Pure function: the loader * builds it from `leadService.getLeadStatistics` (total/new/converted/spam + * by-source + by-status breakdowns). Richer breakdowns from the designer brief * (reaction time, per-owner, weekday, trends) are NOT yet provided by the service * and stay a tracked design-nachzug — this VM renders exactly what the backend * exposes (no invented aggregates). No React / Prisma / server imports (DoD A.3). */ export type LeadStatKpi = "total" | "new" | "converted" | "spam"; export interface LeadStatKpiTile { readonly key: LeadStatKpi; readonly labelKey: string; readonly value: number; } export interface LeadStatBreakdownRow { readonly key: string; readonly labelKey: string; readonly count: number; } export interface LeadStatisticsData { readonly kpis: readonly LeadStatKpiTile[]; readonly byQuelle: readonly LeadStatBreakdownRow[]; readonly byStatus: readonly LeadStatBreakdownRow[]; /** Verantwortlichen-breakdown is only shown with leads.read_all (brief). */ readonly canReadAll: boolean; readonly listHref: string; } export type LeadStatisticsVM = VMState; export interface BuildLeadStatisticsVMInput { readonly stats: { readonly total: number; readonly new: number; readonly converted: number; readonly spam: number; readonly byQuelle: readonly { readonly quelle: LeadQuelle; readonly count: number }[]; readonly byStatus: readonly { readonly status: LeadStatus; readonly count: number }[]; }; readonly canReadAll: boolean; readonly listHref: string; } export function buildLeadStatisticsVM(input: BuildLeadStatisticsVMInput): LeadStatisticsVM { const { stats } = input; const kpis: LeadStatKpiTile[] = [ { key: "total", labelKey: "leads.statistics.card.total", value: stats.total }, { key: "new", labelKey: "leads.statistics.card.new", value: stats.new }, { key: "converted", labelKey: "leads.statistics.card.converted", value: stats.converted }, { key: "spam", labelKey: "leads.statistics.card.spam", value: stats.spam }, ]; return ready({ kpis, byQuelle: stats.byQuelle.map((row) => ({ key: row.quelle, labelKey: leadQuelleLabelKey(row.quelle), count: row.count })), byStatus: stats.byStatus.map((row) => ({ key: row.status, labelKey: leadStatusLabelKey(row.status), count: row.count })), canReadAll: input.canReadAll, listHref: input.listHref, }); }