module.exports = (sequelize, DataTypes) => {
  const GeneralDCItem = sequelize.define('GeneralDCItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    general_dc_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'GeneralDC', key: 'id' }
    },
    line_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    machine_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'Machine', key: 'id' }
    },
    machine_work: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    sent_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    line_total: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    received_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    rejection_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    storage_location_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'StorageLocation', key: 'id' }
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'general_dc_item',
    indexes: [
      { fields: ['general_dc_id'] },
      { fields: ['machine_id'] },
      { fields: ['storage_location_id'] },
      { unique: true, fields: ['general_dc_id', 'line_number'] }
    ]
  });

  GeneralDCItem.associate = function(models) {
    GeneralDCItem.belongsTo(models.GeneralDC, { foreignKey: 'general_dc_id', as: 'generalDC' });
    GeneralDCItem.belongsTo(models.Machine, { foreignKey: 'machine_id', as: 'machine' });
    GeneralDCItem.belongsTo(models.StorageLocation, { foreignKey: 'storage_location_id', as: 'storageLocation' });
    GeneralDCItem.hasMany(models.GeneralReceiptItem, { foreignKey: 'general_dc_item_id', as: 'receiptItems' });
  };

  return GeneralDCItem;
};
