import 'dotenv/config';
import bcrypt from 'bcryptjs';
import { db } from './db.js';

const username = process.env.ADMIN_USERNAME || 'admin';
const password = process.env.ADMIN_PASSWORD || 'admin1234';

const existing = db.prepare('SELECT id FROM users WHERE username = ?').get(username);
if (existing) {
  console.log(`User "${username}" already exists (id=${existing.id}). Skipping.`);
  process.exit(0);
}

const hash = bcrypt.hashSync(password, 10);
const info = db
  .prepare('INSERT INTO users (username, password_hash) VALUES (?, ?)')
  .run(username, hash);

console.log(`Created admin user "${username}" (id=${info.lastInsertRowid}).`);
console.log(`Login with the password from your .env (ADMIN_PASSWORD).`);
