const mongoose = require('mongoose');
const dotenv = require('dotenv');
const User = require('../models/User');

// Load env vars
dotenv.config();

// Connect to DB
mongoose.connect(process.env.MONGO_URI)
  .then(() => console.log('MongoDB Connected'))
  .catch(err => {
    console.error('Error connecting to MongoDB:', err.message);
    process.exit(1);
  });

const createAdminAccount = async () => {
  try {
    // Check if admin already exists - use a valid email format
    const adminExists = await User.findOne({ email: 'admin@example.com' });
    
    if (adminExists) {
      console.log('Admin account already exists');
      console.log('Email: admin@example.com');
      console.log('Password: admin124');
      process.exit(0);
    }
    
    // Create admin user with a valid email format
    const admin = new User({
      name: 'Администратор',
      email: 'admin@example.com',
      password: 'admin124',
      dateOfBirth: new Date('1990-01-01'),
      gender: 'other',
      isActive: true,
      isAdmin: true,
      location: {
        city: 'Admin',
        country: 'System'
      }    });
    
    await admin.save();
    console.log('Admin account created successfully');
    console.log('Email: admin@example.com');
    console.log('Password: admin124');
    process.exit(0);
  } catch (error) {
    console.error('Error creating admin account:', error);
    process.exit(1);
  }
};

createAdminAccount();
