import 'dotenv/config';
import { and, eq } from 'drizzle-orm';

import { auth } from '@/lib/auth';
import { db } from '@/lib/db';
import { user, account } from '@/lib/db/schema/users';

async function main() {
  const email = process.argv[2] ?? 'admin@portnimara.test';
  const pw = process.argv[3] ?? 'SuperAdmin12345!';
  const [u] = await db.select().from(user).where(eq(user.email, email)).limit(1);
  if (!u) throw new Error(`user not found: ${email}`);
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const ctx = await (auth as any).$context;
  const hash = await ctx.password.hash(pw);
  const res = await db
    .update(account)
    .set({ password: hash })
    .where(and(eq(account.userId, u.id), eq(account.providerId, 'credential')))
    .returning({ id: account.id });
  console.log(`updated ${res.length} credential row(s) for ${email}`);
  process.exit(0);
}

main().catch((e) => {
  console.error(e);
  process.exit(1);
});
