import { n as __toESM } from "../_runtime.mjs";
import { I as require_jsx_runtime } from "../_libs/@radix-ui/react-alert-dialog+[...].mjs";
import { t as PageHeader } from "./page-header-BE1EG5aP.mjs";
import { u as require_react } from "../_libs/@floating-ui/react-dom+[...].mjs";
import { i as CardTitle, n as CardContent, r as CardHeader, t as Card } from "./card-BXjpJ96D.mjs";
import { a as TableHead, n as TableBody, o as TableHeader, r as TableCell, s as TableRow, t as Table } from "./table-BAceZcga.mjs";
import { n as useItems, r as useLatestFacts } from "./data-hooks--e_NaUze.mjs";
import { t as Badge } from "./badge-D1Dupn2y.mjs";
import { t as Button } from "./button-BLZ6ednA.mjs";
import { O as ChevronRight, h as Printer } from "../_libs/lucide-react.mjs";
import { a as weightedCompliance, o as zoneClass, r as compliancePct, s as zoneOf } from "./calculations-JyW9rCxF.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/division-performance-DmXSTcU4.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
var emptyAgg = () => ({
	scope: 0,
	prog: 0,
	wComp: 0,
	weight: 0
});
var pct = (a) => a.scope > 0 ? a.prog / a.scope * 100 : 0;
var wPct = (a) => a.weight > 0 ? a.wComp / a.weight : 0;
function DivisionPerformance() {
	const { data: facts = [] } = useLatestFacts("SSE");
	const { data: items = [] } = useItems();
	const wMap = (0, import_react.useMemo)(() => new Map(items.map((i) => [i.sr_no, i.weightage ?? 0])), [items]);
	const tree = (0, import_react.useMemo)(() => {
		const divs = /* @__PURE__ */ new Map();
		for (const r of facts) {
			const w = wMap.get(r.item_sr_no) ?? r.weightage ?? 0;
			const wc = weightedCompliance(compliancePct(r.cum_progress, r.cum_scope), w);
			if (!divs.has(r.division)) divs.set(r.division, {
				agg: emptyAgg(),
				aens: /* @__PURE__ */ new Map()
			});
			const d = divs.get(r.division);
			d.agg.scope += r.cum_scope;
			d.agg.prog += r.cum_progress;
			d.agg.wComp += wc;
			d.agg.weight += w;
			if (!d.aens.has(r.aen)) d.aens.set(r.aen, {
				agg: emptyAgg(),
				sses: /* @__PURE__ */ new Map()
			});
			const a = d.aens.get(r.aen);
			a.agg.scope += r.cum_scope;
			a.agg.prog += r.cum_progress;
			a.agg.wComp += wc;
			a.agg.weight += w;
			const sse = r.sse ?? "—";
			if (!a.sses.has(sse)) a.sses.set(sse, emptyAgg());
			const s = a.sses.get(sse);
			s.scope += r.cum_scope;
			s.prog += r.cum_progress;
			s.wComp += wc;
			s.weight += w;
		}
		return divs;
	}, [facts, wMap]);
	const divisions = (0, import_react.useMemo)(() => Array.from(tree.entries()).map(([division, d]) => ({
		division,
		compliance: pct(d.agg),
		wComp: wPct(d.agg),
		aens: d.aens.size
	})).sort((a, b) => b.wComp - a.wComp), [tree]);
	const [selDiv, setSelDiv] = (0, import_react.useState)();
	const [selAen, setSelAen] = (0, import_react.useState)();
	const aensForDiv = (0, import_react.useMemo)(() => {
		if (!selDiv) return [];
		const d = tree.get(selDiv);
		if (!d) return [];
		const totalW = d.agg.wComp || 1;
		return Array.from(d.aens.entries()).map(([aen, a]) => ({
			aen,
			compliance: pct(a.agg),
			wComp: wPct(a.agg),
			contribution: a.agg.wComp / totalW * 100,
			sses: a.sses.size
		})).sort((a, b) => b.contribution - a.contribution);
	}, [tree, selDiv]);
	const ssesForAen = (0, import_react.useMemo)(() => {
		if (!selDiv || !selAen) return [];
		const a = tree.get(selDiv)?.aens.get(selAen);
		if (!a) return [];
		const totalW = a.agg.wComp || 1;
		return Array.from(a.sses.entries()).map(([sse, s]) => ({
			sse,
			compliance: pct(s),
			wComp: wPct(s),
			contribution: s.wComp / totalW * 100
		})).sort((a, b) => b.contribution - a.contribution);
	}, [
		tree,
		selDiv,
		selAen
	]);
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: "p-6 max-w-[1600px] mx-auto print:p-0",
		children: [
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageHeader, {
				title: "Division Performance",
				subtitle: "Drill down: Division → AEN → SSE contribution to compliance."
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
				className: "flex items-center gap-2 mb-4 text-sm text-muted-foreground print:hidden",
				children: [
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
						className: "hover:text-foreground",
						onClick: () => {
							setSelDiv(void 0);
							setSelAen(void 0);
						},
						children: "All Divisions"
					}),
					selDiv && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ChevronRight, { className: "h-4 w-4" }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
						className: "hover:text-foreground",
						onClick: () => setSelAen(void 0),
						children: selDiv
					})] }),
					selAen && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ChevronRight, { className: "h-4 w-4" }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
						className: "text-foreground",
						children: selAen
					})] }),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "ml-auto",
						children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Button, {
							size: "sm",
							variant: "outline",
							onClick: () => window.print(),
							children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Printer, { className: "h-4 w-4 mr-1" }), " Print"]
						})
					})
				]
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, {
				className: "mb-4 bg-muted/30",
				children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, {
					className: "pb-2",
					children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, {
						className: "text-sm",
						children: "How these numbers are calculated"
					})
				}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardContent, {
					className: "pt-0 text-xs space-y-1 font-mono leading-relaxed",
					children: [
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("b", { children: "Compliance %" }),
							" = Σ(cum_progress) / Σ(cum_scope) × 100 \xA0 ",
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
								className: "font-sans text-muted-foreground",
								children: "(capped at 100)"
							})
						] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("b", { children: "Weighted %" }),
							" = Σ(compliance",
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("sub", { children: "i" }),
							" × weight",
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("sub", { children: "i" }),
							") / Σ(weight",
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("sub", { children: "i" }),
							") \xA0 ",
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
								className: "font-sans text-muted-foreground",
								children: "— risk-adjusted score"
							})
						] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("b", { children: "Contribution % (AEN → Div)" }), " = AEN.Σ(comp × w) / Division.Σ(comp × w) × 100"] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("b", { children: "Contribution % (SSE → AEN)" }), " = SSE.Σ(comp × w) / AEN.Σ(comp × w) × 100"] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
							className: "font-sans text-muted-foreground pt-1",
							children: "Zones: ≥90 green · ≥70 yellow · ≥50 orange · <50 red. Weights come from items_master."
						})
					]
				})]
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
				className: "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-3 mb-6",
				children: divisions.map((d) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, {
					className: `cursor-pointer transition hover:shadow-md ${selDiv === d.division ? "ring-2 ring-primary" : ""}`,
					onClick: () => {
						setSelDiv(d.division);
						setSelAen(void 0);
					},
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, {
						className: "pb-2",
						children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardTitle, {
							className: "text-base flex items-center justify-between",
							children: [d.division, /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
								className: `text-xs px-2 py-0.5 rounded ${zoneClass(d.compliance)}`,
								children: zoneOf(d.compliance).toUpperCase()
							})]
						})
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardContent, {
						className: "pt-0",
						children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
							className: "text-2xl font-bold",
							children: [d.compliance.toFixed(1), "%"]
						}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
							className: "text-xs text-muted-foreground mt-1",
							children: [
								"Weighted: ",
								d.wComp.toFixed(1),
								"% · ",
								d.aens,
								" AENs"
							]
						})]
					})]
				}, d.division))
			}),
			selDiv && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, {
				className: "mb-6",
				children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardTitle, { children: ["AEN Contribution — ", selDiv] }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, {
					className: "p-0",
					children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Table, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Rank" }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "AEN" }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
							className: "text-right",
							children: "Compliance"
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
							className: "text-right",
							children: "Weighted"
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
							className: "text-right",
							children: "Contribution %"
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
							className: "text-right",
							children: "SSEs"
						})
					] }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableBody, { children: [aensForDiv.map((r, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, {
						onClick: () => setSelAen(r.aen),
						className: `cursor-pointer hover:bg-muted/50 ${selAen === r.aen ? "bg-muted" : ""}`,
						children: [
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Badge, {
								variant: "outline",
								children: ["#", i + 1]
							}) }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "font-medium",
								children: r.aen
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", {
									className: `inline-block rounded px-2 py-0.5 text-xs ${zoneClass(r.compliance)}`,
									children: [r.compliance.toFixed(1), "%"]
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
								className: "text-right",
								children: [r.wComp.toFixed(1), "%"]
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
								className: "text-right font-semibold",
								children: [r.contribution.toFixed(1), "%"]
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: r.sses
							})
						]
					}, r.aen)), aensForDiv.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableRow, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
						colSpan: 6,
						className: "text-center text-muted-foreground py-4",
						children: "No AEN data"
					}) })] })] })
				})]
			}),
			selDiv && selAen && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardTitle, { children: [
				"SSE Contribution — ",
				selDiv,
				" / ",
				selAen
			] }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, {
				className: "p-0",
				children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Table, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Rank" }),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "SSE" }),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
						className: "text-right",
						children: "Compliance"
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
						className: "text-right",
						children: "Weighted"
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
						className: "text-right",
						children: "Contribution %"
					})
				] }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableBody, { children: [ssesForAen.map((r, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Badge, {
						variant: "outline",
						children: ["#", i + 1]
					}) }),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
						className: "font-medium",
						children: r.sse
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
						className: "text-right",
						children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", {
							className: `inline-block rounded px-2 py-0.5 text-xs ${zoneClass(r.compliance)}`,
							children: [r.compliance.toFixed(1), "%"]
						})
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
						className: "text-right",
						children: [r.wComp.toFixed(1), "%"]
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
						className: "text-right font-semibold",
						children: [r.contribution.toFixed(1), "%"]
					})
				] }, r.sse)), ssesForAen.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableRow, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
					colSpan: 5,
					className: "text-center text-muted-foreground py-4",
					children: "No SSE data"
				}) })] })] })
			})] })
		]
	});
}
//#endregion
export { DivisionPerformance as component };
