'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const [companies, users] = await Promise.all([
      queryInterface.sequelize.query("SELECT id FROM company WHERE code = 'COMP001' LIMIT 1"),
      queryInterface.sequelize.query("SELECT id FROM user WHERE username = 'admin' LIMIT 1")
    ]);

    if (companies[0].length === 0 || users[0].length === 0) return;

    const companyId = companies[0][0].id;
    const userId = users[0][0].id;

    await queryInterface.bulkInsert('designation', [
      { company_id: companyId, designation_code: 'CEO', designation_name: 'Chief Executive Officer', grade: 'L1', level: 1, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'MD', designation_name: 'Managing Director', grade: 'L1', level: 1, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'GM', designation_name: 'General Manager', grade: 'L2', level: 2, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'DGM', designation_name: 'Deputy General Manager', grade: 'L3', level: 3, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'MGR', designation_name: 'Manager', grade: 'L4', level: 4, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'SR_MGR', designation_name: 'Senior Manager', grade: 'L4', level: 4, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'ASST_MGR', designation_name: 'Assistant Manager', grade: 'L5', level: 5, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'EXEC', designation_name: 'Executive', grade: 'L6', level: 6, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'SR_EXEC', designation_name: 'Senior Executive', grade: 'L6', level: 6, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() },
      { company_id: companyId, designation_code: 'OPR', designation_name: 'Operator', grade: 'L7', level: 7, status: 'ACTIVE', created_by: userId, created_at: new Date(), updated_at: new Date() }
    ], { ignoreDuplicates: true });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('designation', null, {});
  }
};

