const Database = require('better-sqlite3');
const path = require('path');
const bcrypt = require('bcryptjs');

const dbPath = path.join(__dirname, '../data/fitai.db');
const db = new Database(dbPath);

async function seedSuperAdmin() {
    const email = 'taratur@gmail.com';
    const password = 'password123';
    const firstName = 'Super';
    const lastName = 'Admin';

    // Hash password
    const hashedPassword = await bcrypt.hash(password, 12);

    const id = 'user_superadmin_' + Math.random().toString(36).substr(2, 9);
    const now = new Date().toISOString();

    try {
        console.log('Creating Super Admin...');

        // Check if exists
        const existing = db.prepare('SELECT * FROM users WHERE email = ?').get(email);
        if (existing) {
            console.log('Super Admin already exists. Updating role...');
            db.prepare('UPDATE users SET role = "superAdmin" WHERE email = ?').run(email);
            console.log('Role updated.');
            return;
        }

        db.prepare(`
      INSERT INTO users (id, email, firstName, lastName, password, role, createdAt, updatedAt)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    `).run(id, email, firstName, lastName, hashedPassword, 'superAdmin', now, now);

        console.log(`Super Admin created successfully.`);
        console.log(`Email: ${email}`);
        console.log(`Password: ${password}`);
        console.log(`ID: ${id}`);

        console.log('\nIMPORTANT: You must also create this user in Clerk manually or sign up with this email to link the accounts if you want to log in as this user.');

    } catch (error) {
        console.error('Error creating Super Admin:', error);
    } finally {
        db.close();
    }
}

seedSuperAdmin();
