module.exports = (sequelize, DataTypes) => {
  const MaterialSubstitutionTransaction = sequelize.define('MaterialSubstitutionTransaction', {
    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'
      }
    },
    substitution_master_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'MaterialSubstitutionMaster',
        key: 'id'
      },
      comment: 'Reference to substitution master rule (if applicable)'
    },
    reference_type: {
      type: DataTypes.ENUM('PRODUCTION_ORDER', 'MATERIAL_REQUEST', 'BOM', 'OTHER'),
      allowNull: false,
      comment: 'Type of document where substitution occurred'
    },
    reference_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'ID of the document (Production Order, Material Request, etc.)'
    },
    reference_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'ID of the item within the document (ProductionOrderComponent, MaterialRequestItem, etc.)'
    },
    original_material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    substitute_material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    original_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      comment: 'Original quantity required'
    },
    substitute_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      comment: 'Substitute quantity used'
    },
    substitution_ratio: {
      type: DataTypes.DECIMAL(10, 4),
      allowNull: false,
      comment: 'Ratio used for this substitution'
    },
    substitution_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    substitution_reason: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: 'Reason for substitution'
    },
    approval_status: {
      type: DataTypes.ENUM('PENDING', 'APPROVED', 'REJECTED', 'NOT_REQUIRED'),
      allowNull: false,
      defaultValue: 'PENDING'
    },
    approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    approval_remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'CANCELLED'),
      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_transaction',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['reference_type', 'reference_id']
      },
      {
        fields: ['original_material_id']
      },
      {
        fields: ['substitute_material_id']
      },
      {
        fields: ['approval_status']
      },
      {
        fields: ['substitution_date']
      }
    ]
  });

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

  return MaterialSubstitutionTransaction;
};

