'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('material_substitution_master', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      original_material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'Original material that can be substituted'
      },
      substitute_material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'Material that can substitute the original'
      },
      substitution_ratio: {
        type: Sequelize.DECIMAL(10, 4),
        allowNull: false,
        defaultValue: 1.0000,
        comment: 'Ratio of substitute to original (e.g., 1.2 means 1.2 units of substitute = 1 unit of original)'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Date from which substitution is valid'
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Date until which substitution is valid'
      },
      requires_approval: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: 'Whether substitution requires approval'
      },
      approval_level: {
        type: Sequelize.ENUM('NONE', 'SUPERVISOR', 'MANAGER', 'DIRECTOR'),
        allowNull: false,
        defaultValue: 'SUPERVISOR',
        comment: 'Required approval level'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    // Add indexes
    await queryInterface.addIndex('material_substitution_master', ['company_id', 'plant_id'], {
      name: 'material_substitution_company_plant_idx'
    });
    await queryInterface.addIndex('material_substitution_master', ['original_material_id'], {
      name: 'material_substitution_original_material_idx'
    });
    await queryInterface.addIndex('material_substitution_master', ['substitute_material_id'], {
      name: 'material_substitution_substitute_material_idx'
    });
    await queryInterface.addIndex('material_substitution_master', ['status'], {
      name: 'material_substitution_status_idx'
    });
    await queryInterface.addIndex('material_substitution_master', ['effective_from', 'effective_to'], {
      name: 'material_substitution_dates_idx'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('material_substitution_master');
  }
};

