import dotenv from 'dotenv';
import mongoose from 'mongoose';
import path from 'path';
import User from '../models/User';

dotenv.config({ path: path.resolve(__dirname, '../../.env') });

async function createOrUpdateAdmin() {
    const mongoUri = process.env.MONGODB_URI!;
    const email = 'admin@gmail.com';
    const password = 'Admin1234'; // You may want to hash this password in a real application
    const firstName = 'Admin';
    const lastName = 'User';
    const language = 'es';

    try {
        await mongoose.connect(mongoUri);
        console.log('Connected to MongoDB');

        let admin = await User.findOne({ email });
        if (!admin) {
            admin = new User({
                firstName,
                lastName,
                email,
                password,
                userType: 'admin',
                isActive: true,
                language
            });
            await admin.save();
            console.log(`Admin created: ${email}`);
        } else {
            admin.firstName = firstName;
            admin.lastName = lastName;
            admin.userType = 'admin';
            admin.isActive = true;
            admin.language = language;
            admin.password = password;
            await admin.save();
            console.log(`Admin updated: ${email}`);
        }

        console.log('Done.');
    } catch (error) {
        console.error('Failed to create/update admin:', error);
        process.exitCode = 1;
    } finally {
        await mongoose.disconnect();
    }
}

createOrUpdateAdmin();
