module.exports = (sequelize, DataTypes) => {
  const MaterialSubstitutionMaster = sequelize.define('MaterialSubstitutionMaster', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    original_material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      },
      comment: 'Original material that can be substituted'
    },
    substitute_material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      },
      comment: 'Material that can substitute the original'
    },
    substitution_ratio: {
      type: DataTypes.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: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Date from which substitution is valid'
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Date until which substitution is valid'
    },
    requires_approval: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: 'Whether substitution requires approval'
    },
    approval_level: {
      type: DataTypes.ENUM('NONE', 'SUPERVISOR', 'MANAGER', 'DIRECTOR'),
      allowNull: false,
      defaultValue: 'SUPERVISOR',
      comment: 'Required approval level'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'material_substitution_master',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['original_material_id']
      },
      {
        fields: ['substitute_material_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  MaterialSubstitutionMaster.associate = function(models) {
    MaterialSubstitutionMaster.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    MaterialSubstitutionMaster.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    MaterialSubstitutionMaster.belongsTo(models.Material, { foreignKey: 'original_material_id', as: 'originalMaterial' });
    MaterialSubstitutionMaster.belongsTo(models.Material, { foreignKey: 'substitute_material_id', as: 'substituteMaterial' });
    MaterialSubstitutionMaster.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    MaterialSubstitutionMaster.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    MaterialSubstitutionMaster.hasMany(models.MaterialSubstitutionTransaction, { foreignKey: 'substitution_master_id', as: 'transactions' });
  };

  return MaterialSubstitutionMaster;
};

