import { d as db } from '../../chunks/db.js-le-jjQPv.js';

const load = async () => {
  const counts = db.prepare(
    `SELECT
				COUNT(*) AS total,
				SUM(CASE WHEN status = 'disponible' THEN 1 ELSE 0 END) AS disponibles,
				SUM(CASE WHEN status = 'prestado' THEN 1 ELSE 0 END) AS prestados,
				SUM(CASE WHEN status = 'mantenimiento' THEN 1 ELSE 0 END) AS mantenimiento,
				SUM(CASE WHEN status = 'baja' THEN 1 ELSE 0 END) AS baja
			 FROM devices`
  ).get();
  const overdue = db.prepare(
    "SELECT COUNT(*) AS n FROM loans WHERE returned_at IS NULL AND due_at IS NOT NULL AND due_at < datetime('now')"
  ).get();
  const activeLoans = db.prepare(
    `SELECT l.id, l.device_id, d.code AS device_code, d.name AS device_name,
					c.name AS category_name, l.borrower_name, l.user_id, l.checkout_at,
					l.due_at, l.returned_at, l.notes,
					op.name AS checked_out_by_name
			 FROM loans l
			 JOIN devices d ON d.id = l.device_id
			 LEFT JOIN categories c ON c.id = d.category_id
			 LEFT JOIN users op ON op.id = l.checked_out_by
			 WHERE l.returned_at IS NULL
			 ORDER BY (l.due_at IS NULL), l.due_at ASC, l.checkout_at DESC
			 LIMIT 8`
  ).all();
  const recent = db.prepare(
    `SELECT l.id, l.device_id, d.code AS device_code, d.name AS device_name,
					c.name AS category_name, l.borrower_name, l.user_id, l.checkout_at,
					l.due_at, l.returned_at, l.notes,
					op.name AS checked_out_by_name
			 FROM loans l
			 JOIN devices d ON d.id = l.device_id
			 LEFT JOIN categories c ON c.id = d.category_id
			 LEFT JOIN users op ON op.id = l.checked_out_by
			 ORDER BY COALESCE(l.returned_at, l.checkout_at) DESC
			 LIMIT 6`
  ).all();
  return {
    counts: {
      total: counts.total ?? 0,
      disponibles: counts.disponibles ?? 0,
      prestados: counts.prestados ?? 0,
      mantenimiento: counts.mantenimiento ?? 0,
      baja: counts.baja ?? 0,
      overdue: overdue.n
    },
    activeLoans,
    recent
  };
};

var _page_server_ts = /*#__PURE__*/Object.freeze({
	__proto__: null,
	load: load
});

export { _page_server_ts as _ };
//# sourceMappingURL=_page.server.ts.js-WxbSxSaI.js.map
