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 { r as compliancePct } from "./calculations-JyW9rCxF.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/alerts-B95cRnI9.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
function Alerts() {
	const { data: facts = [] } = useLatestFacts("AEN");
	const { data: items = [] } = useItems();
	const wMap = (0, import_react.useMemo)(() => new Map(items.map((i) => [i.sr_no, i.weightage ?? 0])), [items]);
	const alerts = (0, import_react.useMemo)(() => {
		return facts.map((r) => {
			const w = wMap.get(r.item_sr_no) ?? r.weightage ?? 0;
			const c = compliancePct(r.cum_progress, r.cum_scope);
			let sev = null;
			if (w >= 1 && c < 60) sev = "red";
			else if (w === .5 && c < 75) sev = "amber";
			return {
				...r,
				weight: w,
				compliance: c,
				severity: sev
			};
		}).filter((r) => r.severity !== null).sort((a, b) => (a.severity === "red" ? -1 : 1) - (b.severity === "red" ? -1 : 1) || a.compliance - b.compliance);
	}, [facts, wMap]);
	const red = alerts.filter((a) => a.severity === "red");
	const amber = alerts.filter((a) => a.severity === "amber");
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: "p-6 max-w-[1600px] mx-auto",
		children: [
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageHeader, {
				title: "Alerts & Exceptions",
				subtitle: "Automatically generated based on risk weight and compliance thresholds."
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
				className: "grid grid-cols-2 gap-4 mb-4",
				children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, {
					className: "border-destructive/50",
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, {
						className: "text-destructive",
						children: "Red alerts"
					}) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardContent, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "text-3xl font-semibold",
						children: red.length
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "text-xs text-muted-foreground mt-1",
						children: "Weight ≥ 1 and compliance < 60%"
					})] })]
				}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, {
					className: "border-warning/40",
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, {
						style: { color: "oklch(0.55 0.18 45)" },
						children: "Amber alerts"
					}) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardContent, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "text-3xl font-semibold",
						children: amber.length
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "text-xs text-muted-foreground mt-1",
						children: "Weight = 0.5 and compliance < 75%"
					})] })]
				})]
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, { children: "Open alerts" }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, {
				className: "p-0",
				children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
					className: "max-h-[65vh] overflow-auto",
					children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Table, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHeader, {
						className: "sticky top-0 bg-card z-10",
						children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Severity" }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Item" }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Division" }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "AEN" }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Weight"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Compliance"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Cum Scope"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Cum Progress"
							})
						] })
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableBody, { children: [alerts.map((a) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: a.severity === "red" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Badge, {
							variant: "destructive",
							children: "RED"
						}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Badge, {
							style: {
								backgroundColor: "oklch(0.72 0.18 45)",
								color: "white"
							},
							children: "AMBER"
						}) }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
							className: "max-w-[280px]",
							children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
								className: "text-xs text-muted-foreground",
								children: a.item_sr_no
							}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
								className: "truncate",
								children: a.item_name
							})]
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: a.division }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: a.aen }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
							className: "text-right",
							children: a.weight.toFixed(2)
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
							className: "text-right",
							children: [a.compliance.toFixed(1), "%"]
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
							className: "text-right",
							children: a.cum_scope
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
							className: "text-right",
							children: a.cum_progress
						})
					] }, a.id)), alerts.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableRow, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
						colSpan: 8,
						className: "text-center text-sm text-muted-foreground py-8",
						children: "No open alerts."
					}) })] })] })
				})
			})] })
		]
	});
}
//#endregion
export { Alerts as component };
