module.exports = (sequelize, DataTypes) => {
  const BatchMaster = sequelize.define('BatchMaster', {
    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'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Unique batch number for this material'
    },
    lot_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Lot number (may be same as batch or different)'
    },
    manufacturing_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Date when batch was manufactured'
    },
    expiry_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Expiry date for the batch'
    },
    mfg_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Manufacturing date (alternative field)'
    },
    vendor_batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Vendor batch number if different from internal batch'
    },
    initial_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0.000,
      comment: 'Initial quantity when batch was created'
    },
    current_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0.000,
      comment: 'Current available quantity for this batch'
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Primary storage location for this batch'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'EXPIRED', 'CONSUMED', 'QUARANTINED', 'BLOCKED'),
      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: 'batch_master',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['material_id']
      },
      {
        fields: ['batch_number']
      },
      {
        fields: ['status']
      },
      {
        fields: ['expiry_date']
      },
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'material_id', 'batch_number']
      }
    ]
  });

  BatchMaster.associate = function(models) {
    BatchMaster.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    BatchMaster.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    BatchMaster.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    BatchMaster.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    BatchMaster.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return BatchMaster;
};

