#!/usr/bin/env node
/**
 * scripts/set-test-passwords.js
 *
 * Sets known passwords for the two real test accounts in the local DB:
 *   - admin@yourdomain.com  (superadmin, agency plan)
 *   - nisali@client.local   (user, agency plan)
 *
 * Usage:  node scripts/set-test-passwords.js
 *
 * Both passwords satisfy the policy (10+ chars, number, symbol).
 * Print them at the end so the operator can copy them into their notes.
 */
'use strict';

require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') });
const User  = require('../models/user');
const Audit = require('../models/auditLog');
const { db, init: initDb } = require('../config/db');

const ADMIN_PW  = process.env.TEST_ADMIN_PW  || 'ShieldAdm1n!2026';
const USER_PW   = process.env.TEST_USER_PW   || 'NisaliUs3r!2026';
const ADMIN_EMAIL = 'admin@yourdomain.com';
const USER_EMAIL  = 'nisali@client.local';

db.init;
initDb();

const accounts = [
  { email: ADMIN_EMAIL, password: ADMIN_PW,  role: 'superadmin' },
  { email: USER_EMAIL,  password: USER_PW,   role: 'user' },
];

for (const a of accounts) {
  const u = User.getByEmail(a.email);
  if (!u) {
    console.error(`[skip] ${a.email} not found in DB`);
    continue;
  }
  User.setPassword(u.id, a.password);
  // Ensure email_verified and not suspended — both must be true to log in
  if (!u.email_verified) {
    db.prepare('UPDATE users SET email_verified = 1 WHERE id = ?').run(u.id);
  }
  Audit.log(u, 'test.password_reset', `user:${u.id}`);
  console.log(`[ok] ${a.email}  (id=${u.id}, role=${u.role})`);
}

console.log('\n=== TEST CREDENTIALS ===');
console.log('superadmin:  ' + ADMIN_EMAIL + '  /  ' + ADMIN_PW);
console.log('user:        ' + USER_EMAIL  + '  /  ' + USER_PW);
console.log('========================');
console.log('Both passwords satisfy policy: >=10 chars, number, symbol.');