'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Get RAM Industries company ID
    const [companies, users] = await Promise.all([
      queryInterface.sequelize.query(
        "SELECT id FROM company WHERE name LIKE '%RAM Industries%' OR code = 'RI' OR code = 'COMP001' LIMIT 1"
      ),
      queryInterface.sequelize.query("SELECT id FROM user WHERE username = 'admin' LIMIT 1")
    ]);

    const companyId = companies[0].length > 0 ? companies[0][0].id : null;
    const userId = users[0].length > 0 ? users[0][0].id : null;

    if (!companyId) {
      console.log('Company not found. Skipping department seeding.');
      return;
    }

    console.log(`Seeding departments for company ID: ${companyId}`);

    const departments = [
      // Top Management
      {
        company_id: companyId,
        department_code: 'MGMT',
        department_name: 'Management',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      // Production & Manufacturing
      {
        company_id: companyId,
        department_code: 'PROD',
        department_name: 'Production',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'MFG',
        department_name: 'Manufacturing',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'WELD',
        department_name: 'Welding',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'ASSY',
        department_name: 'Assembly',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'FAB',
        department_name: 'Fabrication',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      // Quality & Engineering
      {
        company_id: companyId,
        department_code: 'QA',
        department_name: 'Quality Assurance',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'QC',
        department_name: 'Quality Control',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'ENG',
        department_name: 'Engineering',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'R&D',
        department_name: 'Research & Development',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      // Supply Chain
      {
        company_id: companyId,
        department_code: 'PUR',
        department_name: 'Purchase',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'STORE',
        department_name: 'Stores & Inventory',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'LOG',
        department_name: 'Logistics',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      // Sales & Marketing
      {
        company_id: companyId,
        department_code: 'SALES',
        department_name: 'Sales',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'MKT',
        department_name: 'Marketing',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      // Support Functions
      {
        company_id: companyId,
        department_code: 'FIN',
        department_name: 'Finance',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'ACC',
        department_name: 'Accounts',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'HR',
        department_name: 'Human Resources',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'IT',
        department_name: 'Information Technology',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'MAINT',
        department_name: 'Maintenance',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'ADMIN',
        department_name: 'Administration',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        department_code: 'SEC',
        department_name: 'Security',
        parent_department_id: null,
        status: 'ACTIVE',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      }
    ];

    await queryInterface.bulkInsert('department', departments, {
      ignoreDuplicates: true
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('department', {
      department_code: [
        'MGMT', 'PROD', 'MFG', 'WELD', 'ASSY', 'FAB',
        'QA', 'QC', 'ENG', 'R&D',
        'PUR', 'STORE', 'LOG',
        'SALES', 'MKT',
        'FIN', 'ACC', 'HR', 'IT', 'MAINT', 'ADMIN', 'SEC'
      ]
    }, {});
  }
};

