// Migration script to invalidate old password hashes
// Old format "hash_..." passwords will be invalidated, forcing password reset

import { sql } from "../db";

async function migratePasswords() {
  console.log("Finding users with old password hashes...");

  // Find users with old hash format
  const users = await sql`
    SELECT id, email, password_hash FROM users
    WHERE password_hash IS NOT NULL
      AND password_hash LIKE 'hash_%'
  `;

  console.log(`Found ${users.length} users with old password hashes`);

  for (const user of users) {
    console.log(`Invalidating password for ${user.email}...`);
    await sql`
      UPDATE users SET password_hash = NULL, password_updated_at = NULL
      WHERE id = ${user.id}
    `;
  }

  console.log("Migration complete. Users will need to reset password.");
}

migratePasswords().catch(console.error).finally(() => process.exit());