module.exports = (sequelize, DataTypes) => {
  const MaterialIssueReceiveItem = sequelize.define(
    'MaterialIssueReceiveItem',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      material_issue_receive_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      material_issue_item_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      material_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      uom_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      received_quantity: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
      },
      batch_number: {
        type: DataTypes.STRING(50),
        allowNull: true,
      },
      received_from_storage_location: {
        type: DataTypes.STRING(10),
        allowNull: true,
      },
      received_to_storage_location: {
        type: DataTypes.STRING(10),
        allowNull: true,
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      created_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      updated_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
    },
    {
      tableName: 'material_issue_receive_item',
      timestamps: false,
      indexes: [
        { fields: ['material_issue_item_id'] },
        { fields: ['material_issue_receive_id'] },
      ],
    }
  );

  MaterialIssueReceiveItem.associate = function (models) {
    MaterialIssueReceiveItem.belongsTo(models.MaterialIssueReceive, {
      foreignKey: 'material_issue_receive_id',
      as: 'receive',
    });
    MaterialIssueReceiveItem.belongsTo(models.MaterialIssueItem, {
      foreignKey: 'material_issue_item_id',
      as: 'materialIssueItem',
      constraints: false,
    });
    MaterialIssueReceiveItem.belongsTo(models.Material, {
      foreignKey: 'material_id',
      as: 'material',
      constraints: false,
    });
    MaterialIssueReceiveItem.belongsTo(models.UOM, {
      foreignKey: 'uom_id',
      as: 'uom',
      constraints: false,
    });
  };

  return MaterialIssueReceiveItem;
};

