import { C as error, O as fail } from '../../../chunks/utils.js-mIi_c4Oz.js';
import { a as hasRole } from '../../../chunks/auth.js-D502gejJ.js';
import { d as db } from '../../../chunks/db.js-le-jjQPv.js';
import { b as listUsers } from '../../../chunks/queries.js-BUUEc74j.js';

const load = async ({ locals }) => {
  if (!hasRole(locals.user, "admin")) throw error(403, "Solo administración.");
  return { users: listUsers() };
};
const actions = {
  toggleActive: async ({ request, locals }) => {
    if (!hasRole(locals.user, "admin")) throw error(403, "Solo administración.");
    const id = Number((await request.formData()).get("id"));
    if (id === locals.user.id) return fail(400, { error: "No puedes desactivar tu propia cuenta." });
    db.prepare("UPDATE users SET active = 1 - active WHERE id = ?").run(id);
    return { success: true };
  },
  delete: async ({ request, locals }) => {
    if (!hasRole(locals.user, "admin")) throw error(403, "Solo administración.");
    const id = Number((await request.formData()).get("id"));
    if (id === locals.user.id) return fail(400, { error: "No puedes eliminar tu propia cuenta." });
    db.prepare("DELETE FROM users WHERE id = ?").run(id);
    return { success: true };
  }
};

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

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