/**
 * Dev helper: set a user's password directly (bypasses email reset).
 * Usage: pnpm tsx scripts/dev-set-password.ts <email> <password>
 */
import 'dotenv/config';
import { hashPassword } from 'better-auth/crypto';
import { eq, and } from 'drizzle-orm';
import { db } from '@/lib/db';
import { user, account } from '@/lib/db/schema/users';

async function main() {
  const [, , email, password] = process.argv;
  if (!email || !password) {
    console.error('Usage: pnpm tsx scripts/dev-set-password.ts <email> <password>');
    process.exit(1);
  }

  const u = await db.query.user.findFirst({ where: eq(user.email, email) });
  if (!u) {
    console.error(`User not found: ${email}`);
    process.exit(1);
  }

  const hash = await hashPassword(password);
  const result = await db
    .update(account)
    .set({ password: hash, updatedAt: new Date() })
    .where(and(eq(account.userId, u.id), eq(account.providerId, 'credential')))
    .returning({ id: account.id });

  if (result.length === 0) {
    console.error(`No credential account row for ${email}`);
    process.exit(1);
  }

  console.log(`Updated password for ${email} (account id ${result[0]?.id}).`);
  process.exit(0);
}

main();
