'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Get all companies
    const [companies] = await queryInterface.sequelize.query(
      "SELECT id FROM company"
    );

    // Get admin user for created_by
    const [users] = await queryInterface.sequelize.query(
      "SELECT id FROM user WHERE username = 'admin' LIMIT 1"
    );

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

    // Default material types
    const materialTypes = [
      {
        type_code: 'RAW_MAT',
        type_name: 'Raw Material',
        description: 'Raw materials used in production',
        is_active: true
      },
      {
        type_code: 'ASSEMBLY',
        type_name: 'Assembly',
        description: 'Assembled components or sub-assemblies',
        is_active: true
      },
      {
        type_code: 'SEMI_FIN',
        type_name: 'Semi-Finished Good',
        description: 'Partially finished goods or sub-assemblies',
        is_active: true
      },
      {
        type_code: 'FIN_GOOD',
        type_name: 'Finished Good',
        description: 'Completed products ready for sale',
        is_active: true
      },
      {
        type_code: 'PACKAGING',
        type_name: 'Packaging Material',
        description: 'Materials used for packaging finished goods',
        is_active: true
      },
      {
        type_code: 'CONSUMABLE',
        type_name: 'Consumable',
        description: 'Consumable items used in production or operations',
        is_active: true
      },
      {
        type_code: 'SERVICE',
        type_name: 'Service',
        description: 'Service items (non-inventory)',
        is_active: true
      },
      {
        type_code: 'ASSET',
        type_name: 'Asset',
        description: 'Fixed assets and capital goods',
        is_active: true
      }
    ];

    // Insert material types for each company
    for (const company of companies) {
      const companyId = company.id;
      
      for (const materialType of materialTypes) {
        // Check if material type already exists for this company
        const [existing] = await queryInterface.sequelize.query(
          `SELECT id FROM material_type WHERE company_id = :companyId AND type_code = :typeCode LIMIT 1`,
          {
            replacements: { companyId, typeCode: materialType.type_code },
            type: Sequelize.QueryTypes.SELECT
          }
        );

        if (!existing || existing.length === 0) {
          await queryInterface.bulkInsert('material_type', [{
            company_id: companyId,
            type_code: materialType.type_code,
            type_name: materialType.type_name,
            description: materialType.description,
            is_active: materialType.is_active,
            created_by: userId,
            created_at: new Date(),
            updated_at: new Date()
          }]);
        }
      }
    }
  },

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

