module.exports = (sequelize, DataTypes) => {
  const ReceiptEntry = sequelize.define('ReceiptEntry', {
    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_no: { type: DataTypes.STRING(60), allowNull: false },
    receipt_date: { type: DataTypes.DATEONLY, allowNull: false },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    invoice_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'SalesInvoice', key: 'id' }
    },
    delivery_challan_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'DeliveryChallan', key: 'id' }
    },
    invoice_total: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    paid_amount: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    balance_amount: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    payment_mode: {
      type: DataTypes.ENUM('CASH', 'BANK', 'UPI', 'CHEQUE', 'NEFT'),
      allowNull: false,
      defaultValue: 'CASH'
    },
    bank_account: { type: DataTypes.STRING(200), allowNull: true },
    bank_name: { type: DataTypes.STRING(120), allowNull: true },
    ifsc_code: { type: DataTypes.STRING(20), allowNull: true },
    reference_no: { type: DataTypes.STRING(120), allowNull: true },
    advance_amount: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    adjusted_advance_amount: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    collected_from: {
      type: DataTypes.ENUM('DELIVERY', 'OFFICE'),
      allowNull: false,
      defaultValue: 'OFFICE'
    },
    remarks: { type: DataTypes.TEXT, allowNull: true },
    status: {
      type: DataTypes.ENUM('DRAFT', 'POSTED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    posted_at: { type: DataTypes.DATE, allowNull: true },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' }
    },
    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: 'receipt_entry',
    indexes: [
      { unique: true, fields: ['company_id', 'plant_id', 'fiscal_year_id', 'receipt_no'] },
      { fields: ['invoice_id'] },
      { fields: ['customer_id'] },
      { fields: ['delivery_challan_id'] },
      { fields: ['status'] },
      { fields: ['receipt_date'] }
    ]
  });

  ReceiptEntry.associate = function(models) {
    ReceiptEntry.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ReceiptEntry.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ReceiptEntry.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ReceiptEntry.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    ReceiptEntry.belongsTo(models.SalesInvoice, { foreignKey: 'invoice_id', as: 'invoice' });
    ReceiptEntry.belongsTo(models.DeliveryChallan, { foreignKey: 'delivery_challan_id', as: 'deliveryChallan' });
    ReceiptEntry.hasMany(models.ReceiptEntryItem, { foreignKey: 'receipt_entry_id', as: 'items' });
    ReceiptEntry.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ReceiptEntry.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    ReceiptEntry.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
  };

  return ReceiptEntry;
};

