'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Refund extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
     Refund.belongsTo(models.Return, {
        foreignKey: 'returnId',
        as: 'return'
      });
      
      // Association with Transaction
      Refund.belongsTo(models.Transaction, {
        foreignKey: 'transactionId',
        as: 'originalTransaction'
      });
      
      // Association with Customer
      Refund.belongsTo(models.Customer, {
        foreignKey: 'customerId',
        as: 'customer'
      });
      
      // Association with User (processor)
      Refund.belongsTo(models.User, {
        foreignKey: 'processedBy',
        as: 'processor'
      });
    }
  }
  Refund.init({
    returnId: DataTypes.INTEGER,
    transactionId: DataTypes.INTEGER,
    customerId: DataTypes.INTEGER,
    refundNumber: DataTypes.STRING,
    amount: DataTypes.DECIMAL,
    method: DataTypes.STRING,
    status: DataTypes.STRING,
    paymentReference: DataTypes.STRING,
    storeCreditId: DataTypes.INTEGER,
    exchangeTransactionId: DataTypes.INTEGER,
    processedBy: DataTypes.INTEGER,
    processedAt: DataTypes.DATE,
    failureReason: DataTypes.TEXT,
    notes: DataTypes.TEXT,
    metadata: DataTypes.JSON
  }, {
    sequelize,
    modelName: 'Refund',
  });
  return Refund;
};