import { J as jsxRuntimeExports } from "./server-CZT6KZsz.js";
import { u as useSuspenseQuery } from "./useSuspenseQuery-CRUy30GM.js";
import { A as AppShell, W as Wallet } from "./AppShell-CCqmKL3X.js";
import { t as movimientosQO, l as formatARS } from "./router-Bsssaenw.js";
import { T as TrendingUp } from "./trending-up-G3x4lSVY.js";
import { c as createLucideIcon } from "./wrench-CccwRKu3.js";
import "node:async_hooks";
import "node:stream/web";
import "node:stream";
import "./package-D5atLoEx.js";
const __iconNode = [
  ["path", { d: "M16 17h6v-6", key: "t6n2it" }],
  ["path", { d: "m22 17-8.5-8.5-5 5L2 7", key: "x473p" }]
];
const TrendingDown = createLucideIcon("trending-down", __iconNode);
function CuentasPage() {
  const {
    data: movimientos
  } = useSuspenseQuery(movimientosQO());
  const map = /* @__PURE__ */ new Map();
  for (const m of movimientos) {
    const isAdm = !!m.administracion_id;
    const key = m.administracion_id || m.cliente_particular_id;
    const nombre = m.administracion?.nombre ?? m.cliente_particular?.nombre ?? "—";
    const cur = map.get(key) ?? {
      key,
      tipo: isAdm ? "Consorcio" : "Particular",
      nombre,
      saldo: 0,
      ultimoMov: m.descripcion
    };
    const monto = Number(m.monto);
    cur.saldo += m.tipo === "debito" ? monto : -monto;
    map.set(key, cur);
  }
  const cuentas = Array.from(map.values()).sort((a, b) => b.saldo - a.saldo);
  const aCobrar = cuentas.filter((c) => c.saldo > 0).reduce((s, c) => s + c.saldo, 0);
  const aFavor = Math.abs(cuentas.filter((c) => c.saldo < 0).reduce((s, c) => s + c.saldo, 0));
  return /* @__PURE__ */ jsxRuntimeExports.jsx(AppShell, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-6", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-xs uppercase tracking-widest text-muted-foreground font-mono", children: "Finanzas" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("h1", { className: "text-3xl font-bold mt-1", children: "Cuentas Corrientes" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-muted-foreground mt-1", children: "Saldos calculados a partir de los movimientos cargados" })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid md:grid-cols-3 gap-4", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Stat, { tone: "success", icon: TrendingUp, label: "Por cobrar", value: formatARS(aCobrar) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Stat, { tone: "info", icon: TrendingDown, label: "A favor cliente", value: formatARS(aFavor) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Stat, { tone: "primary", icon: Wallet, label: "Cuentas activas", value: String(cuentas.length) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "bg-card border border-border rounded-xl shadow-card overflow-hidden", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("table", { className: "w-full text-sm", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("thead", { children: /* @__PURE__ */ jsxRuntimeExports.jsxs("tr", { className: "text-left text-xs uppercase tracking-wider text-muted-foreground border-b border-border", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("th", { className: "px-4 py-3 font-medium", children: "Cliente" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("th", { className: "px-4 py-3 font-medium", children: "Tipo" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("th", { className: "px-4 py-3 font-medium", children: "Último movimiento" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("th", { className: "px-4 py-3 font-medium text-right", children: "Saldo" })
      ] }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("tbody", { children: [
        cuentas.length === 0 && /* @__PURE__ */ jsxRuntimeExports.jsx("tr", { children: /* @__PURE__ */ jsxRuntimeExports.jsx("td", { colSpan: 4, className: "px-4 py-12 text-center text-sm text-muted-foreground", children: "Sin movimientos cargados." }) }),
        cuentas.map((c) => /* @__PURE__ */ jsxRuntimeExports.jsxs("tr", { className: "border-b border-border last:border-0 hover:bg-muted/40", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("td", { className: "px-4 py-3 font-medium", children: c.nombre }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("td", { className: "px-4 py-3", children: /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: `text-xs px-2 py-0.5 rounded ${c.tipo === "Consorcio" ? "bg-info/10 text-info" : "bg-accent text-accent-foreground"}`, children: c.tipo }) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("td", { className: "px-4 py-3 text-xs text-muted-foreground", children: c.ultimoMov }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("td", { className: `px-4 py-3 text-right font-mono font-semibold ${c.saldo > 0 ? "text-success" : c.saldo < 0 ? "text-destructive" : "text-muted-foreground"}`, children: formatARS(c.saldo) })
        ] }, c.key))
      ] })
    ] }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "bg-card border border-border rounded-xl shadow-card overflow-hidden", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "px-5 py-4 border-b border-border", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("h2", { className: "font-display font-semibold", children: "Movimientos recientes" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-xs text-muted-foreground", children: "Últimos 50 movimientos" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "divide-y divide-border", children: movimientos.slice(0, 10).map((m) => /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "px-5 py-3 flex items-center gap-3 text-sm", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "font-mono text-xs text-muted-foreground w-24 shrink-0", children: m.fecha }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex-1 min-w-0", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "font-medium truncate", children: m.descripcion }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-xs text-muted-foreground", children: m.administracion?.nombre ?? m.cliente_particular?.nombre })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: `font-mono font-semibold ${m.tipo === "debito" ? "text-success" : "text-destructive"}`, children: [
          m.tipo === "debito" ? "+" : "−",
          " ",
          formatARS(Number(m.monto))
        ] })
      ] }, m.id)) })
    ] })
  ] }) });
}
function Stat({
  tone,
  icon: Icon,
  label,
  value
}) {
  const c = tone === "success" ? "text-success" : tone === "info" ? "text-info" : "text-primary";
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "bg-card border border-border rounded-xl shadow-card p-5", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: `flex items-center gap-2 ${c}`, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Icon, { className: "size-4" }),
      " ",
      /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "text-xs uppercase tracking-wider font-mono", children: label })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "font-display font-bold text-2xl mt-2", children: value })
  ] });
}
export {
  CuentasPage as component
};
