module.exports = (sequelize, DataTypes) => {
  const VendorPayment = sequelize.define('VendorPayment', {
    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' } },
    payment_no: { type: DataTypes.STRING(60), allowNull: false },
    payment_date: { type: DataTypes.DATEONLY, allowNull: false },
    vendor_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'BusinessPartner', key: 'id' } },
    purchase_invoice_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'VendorInvoice', key: 'id' } },
    invoice_total: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    paid_amount: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    paid_to_invoice_amount: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    advance_amount: { type: DataTypes.DECIMAL(18, 2), allowNull: false, defaultValue: 0 },
    adjusted_advance_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', 'ONLINE_TRANSFER'),
      allowNull: false,
      defaultValue: 'BANK'
    },
    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 },
    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: 'vendor_payment',
    indexes: [
      { unique: true, fields: ['company_id', 'plant_id', 'fiscal_year_id', 'payment_no'] },
      { fields: ['vendor_id'] },
      { fields: ['purchase_invoice_id'] },
      { fields: ['status'] },
      { fields: ['payment_date'] }
    ]
  });

  VendorPayment.associate = function(models) {
    VendorPayment.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    VendorPayment.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    VendorPayment.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    VendorPayment.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    VendorPayment.belongsTo(models.VendorInvoice, { foreignKey: 'purchase_invoice_id', as: 'purchaseInvoice' });
    VendorPayment.hasMany(models.VendorPaymentItem, { foreignKey: 'vendor_payment_id', as: 'items' });
    VendorPayment.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    VendorPayment.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    VendorPayment.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
  };

  return VendorPayment;
};
