// Seed an initial staff sign-in user. Idempotent (upsert by email).
// Run with the target DATABASE_URL in the environment, e.g.:
//   DATABASE_URL="mysql://..." node apps/api/scripts/seed-user.mjs
// Optional: SEED_EMAIL, SEED_PASSWORD, SEED_NAME (dev defaults below).
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const argon2 = require("argon2");
const { PrismaClient } = require("@jorgecuadros/database");

const email = process.env.SEED_EMAIL || "admin@jorgecuadros.local";
const password = process.env.SEED_PASSWORD || "ChangeMe!2026";
const name = process.env.SEED_NAME || "Administrador";

const prisma = new PrismaClient();

const passwordHash = await argon2.hash(password);
const user = await prisma.user.upsert({
  where: { email },
  update: { passwordHash, active: true, role: "ADMIN", name },
  create: { email, passwordHash, active: true, role: "ADMIN", name },
});
console.log(`seeded user: ${user.email} (role ${user.role})`);
console.log(`  password: ${password}`);
await prisma.$disconnect();
