module.exports = (sequelize, DataTypes) => {
  const MaterialIssueItem = sequelize.define('MaterialIssueItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    issue_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'MaterialIssue',
        key: 'id'
      }
    },
    production_order_component_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'ProductionOrderComponent',
        key: 'id'
      }
    },
    material_request_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'MaterialRequestItem',
        key: 'id'
      },
      comment: 'Link to originating material request item (for MR-based issues)'
    },
    line_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    source_storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Storage location from which materials are issued (stores)'
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Destination storage location where materials should be received (production department)'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'material_issue_item',
    indexes: [
      {
        fields: ['issue_id']
      },
      {
        fields: ['material_id']
      },
      {
        fields: ['production_order_component_id']
      },
      {
        fields: ['material_request_item_id']
      }
    ]
  });

  MaterialIssueItem.associate = function(models) {
    MaterialIssueItem.belongsTo(models.MaterialIssue, { foreignKey: 'issue_id', as: 'issue' });
    MaterialIssueItem.belongsTo(models.ProductionOrderComponent, { foreignKey: 'production_order_component_id', as: 'productionOrderComponent' });
    MaterialIssueItem.belongsTo(models.MaterialRequestItem, { foreignKey: 'material_request_item_id', as: 'materialRequestItem' });
    MaterialIssueItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    MaterialIssueItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    MaterialIssueItem.belongsTo(models.StorageLocation, { foreignKey: 'source_storage_location', targetKey: 'storage_location_code', as: 'sourceStorageLocation', constraints: false });
    MaterialIssueItem.belongsTo(models.StorageLocation, { foreignKey: 'storage_location', targetKey: 'storage_location_code', as: 'destinationStorageLocation', constraints: false });
  };

  return MaterialIssueItem;
};

