module.exports = (sequelize, DataTypes) => {
  const GeneralReceipt = sequelize.define('GeneralReceipt', {
    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' }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'FiscalYear', key: 'id' }
    },
    receipt_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    receipt_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    machine_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'Machine', key: 'id' }
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    general_dc_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'GeneralDC', key: 'id' }
    },
    total_received_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    total_rejection_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'POSTED'
    },
    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: 'general_receipt',
    indexes: [
      { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
      { unique: true, fields: ['company_id', 'receipt_number'] },
      { fields: ['machine_id'] },
      { fields: ['vendor_id'] },
      { fields: ['general_dc_id'] },
      { fields: ['receipt_date'] }
    ]
  });

  GeneralReceipt.associate = function(models) {
    GeneralReceipt.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    GeneralReceipt.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    GeneralReceipt.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    GeneralReceipt.belongsTo(models.Machine, { foreignKey: 'machine_id', as: 'machine' });
    GeneralReceipt.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    GeneralReceipt.belongsTo(models.GeneralDC, { foreignKey: 'general_dc_id', as: 'generalDC' });
    GeneralReceipt.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    GeneralReceipt.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    GeneralReceipt.hasMany(models.GeneralReceiptItem, { foreignKey: 'general_receipt_id', as: 'items' });
  };

  return GeneralReceipt;
};
