const dotenv = require('dotenv');
const mongoose = require('mongoose');

dotenv.config();

const Department = require('./models/Department');
const Position = require('./models/Position');
const Employee = require('./models/Employee');
const User = require('./models/User');

const seed = async () => {
  try {
    await mongoose.connect(process.env.MONGO_URI);
    console.log('Connected to MongoDB');

    await Promise.all([
      Department.deleteMany({}),
      Position.deleteMany({}),
      Employee.deleteMany({}),
      User.deleteMany({})
    ]);
    console.log('Cleared existing data');

    const departments = await Department.insertMany([
      { DepartName: 'Human Resources' },
      { DepartName: 'Information Technology' },
      { DepartName: 'Finance' },
      { DepartName: 'Marketing' },
      { DepartName: 'Operations' }
    ]);
    console.log(`Seeded ${departments.length} departments`);

    const positions = await Position.insertMany([
      { PosName: 'Manager', RequiredQualification: "Bachelor's Degree" },
      { PosName: 'Software Developer', RequiredQualification: "Bachelor's in CS or equivalent" },
      { PosName: 'Financial Analyst', RequiredQualification: 'CPA or equivalent' },
      { PosName: 'Marketing Specialist', RequiredQualification: "Bachelor's in Marketing" },
      { PosName: 'HR Coordinator', RequiredQualification: "Bachelor's in HR" }
    ]);
    console.log(`Seeded ${positions.length} positions`);

    const adminEmployee = await Employee.create({
      EmpFirstName: 'Admin',
      EmpLastName: 'User',
      EmpGender: 'Male',
      EmpDateOfBirth: new Date('1990-01-15'),
      EmpEmail: 'admin@hrms.com',
      EmpTelephone: '+1 234 567 890',
      EmpAddress: '123 Admin St, City',
      EmpHireDate: new Date('2024-01-01'),
      EmpStatus: 'Active',
      Department: departments[1]._id,
      Position: positions[1]._id
    });
    console.log('Seeded admin employee');

    await User.create({
      UserName: 'admin',
      Password: 'admin123',
      Employee: adminEmployee._id,
      role: 'admin'
    });
    console.log('Seeded admin user');

    const employee2 = await Employee.create({
      EmpFirstName: 'Jane',
      EmpLastName: 'Smith',
      EmpGender: 'Female',
      EmpDateOfBirth: new Date('1988-06-20'),
      EmpEmail: 'jane.smith@hrms.com',
      EmpTelephone: '+1 987 654 321',
      EmpAddress: '456 Oak Ave, City',
      EmpHireDate: new Date('2024-03-15'),
      EmpStatus: 'On Leave',
      Department: departments[0]._id,
      Position: positions[4]._id
    });

    await User.create({
      UserName: 'jane',
      Password: 'jane123',
      Employee: employee2._id
    });

    const employee3 = await Employee.create({
      EmpFirstName: 'Bob',
      EmpLastName: 'Johnson',
      EmpGender: 'Male',
      EmpDateOfBirth: new Date('1992-11-08'),
      EmpEmail: 'bob.johnson@hrms.com',
      EmpTelephone: '+1 555 123 456',
      EmpAddress: '789 Pine Rd, City',
      EmpHireDate: new Date('2024-06-01'),
      EmpStatus: 'On Mission',
      Department: departments[1]._id,
      Position: positions[1]._id
    });

    await User.create({
      UserName: 'bob',
      Password: 'bob123',
      Employee: employee3._id
    });

    console.log(`\n========================================`);
    console.log(`  HRMS Seeding Complete!`);
    console.log(`========================================`);
    console.log(`  Default Credentials:`);
    console.log(`  -------------------------------`);
    console.log(`  Admin:  admin / admin123`);
    console.log(`  Jane:   jane   / jane123`);
    console.log(`  Bob:    bob    / bob123`);
    console.log(`========================================\n`);

    process.exit(0);
  } catch (error) {
    console.error('Seed error:', error);
    process.exit(1);
  }
};

seed();
