'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const [companies, plants, costCenters, users] = await Promise.all([
      queryInterface.sequelize.query("SELECT id FROM company WHERE code = 'COMP001' LIMIT 1"),
      queryInterface.sequelize.query("SELECT id FROM plant WHERE code = 'PLANT001' LIMIT 1"),
      queryInterface.sequelize.query("SELECT id FROM cost_center WHERE cost_center_code = 'CC001' 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;
    const plantId = plants[0].length > 0 ? plants[0][0].id : null;
    const costCenterId = costCenters[0].length > 0 ? costCenters[0][0].id : null;

    await queryInterface.bulkInsert('work_center', [
      {
        company_id: companyId,
        plant_id: plantId,
        work_center_code: 'WC001',
        description: 'Machining Center 1',
        work_center_category: 'MACHINE',
        cost_center_id: costCenterId,
        number_of_machines: 1,
        number_of_persons: 2,
        capacity: 8.00,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        plant_id: plantId,
        work_center_code: 'WC002',
        description: 'Assembly Line 1',
        work_center_category: 'ASSEMBLY_LINE',
        cost_center_id: costCenterId,
        number_of_persons: 5,
        capacity: 8.00,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        plant_id: plantId,
        work_center_code: 'WC003',
        description: 'Quality Control',
        work_center_category: 'OTHER',
        cost_center_id: costCenterId,
        number_of_persons: 3,
        capacity: 8.00,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      }
    ]);
  },

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

