'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Return 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) {
      // define association here
    
      Return.belongsTo(models.Customer, { foreignKey: 'customerId' });
      Return.belongsTo(models.Store, { foreignKey: 'storeId' });
      Return.belongsTo(models.Register, { foreignKey: 'registerId' });
      Return.belongsTo(models.User, { as: 'Processor', foreignKey: 'processedBy' });
      Return.belongsTo(models.User, { as: 'Approver', foreignKey: 'approvedBy' });
      Return.hasMany(models.ReturnItem, { foreignKey: 'returnId' });

      Return.hasOne(models.Refund, { foreignKey: 'returnId' });
      Return.belongsTo(models.Transaction, { as: 'originalTransaction', foreignKey: 'transactionId' });

    }
  }
  Return.init({
    transactionId: DataTypes.INTEGER,
    returnNumber: DataTypes.STRING,
    customerId: DataTypes.INTEGER,
    storeId: DataTypes.INTEGER,
    registerId: DataTypes.INTEGER,
    reason: DataTypes.TEXT,
    returnType: DataTypes.STRING,
    status: DataTypes.STRING,
    subtotal: DataTypes.DECIMAL,
    taxAmount: DataTypes.DECIMAL,
    discountAmount: DataTypes.DECIMAL,
    totalAmount: DataTypes.DECIMAL,
    refundMethod: DataTypes.STRING,
    refundAmount: DataTypes.DECIMAL,
    restockingFee: DataTypes.DECIMAL,
    processedBy: DataTypes.INTEGER,
    processedAt: DataTypes.DATE,
    approvedBy: DataTypes.INTEGER,
    approvedAt: DataTypes.DATE,
    rejectionReason: DataTypes.TEXT,
    notes: DataTypes.TEXT,
    receiptUrl: DataTypes.STRING,
    isWithinReturnWindow: DataTypes.BOOLEAN,
    daysFromPurchase: DataTypes.INTEGER
  }, {
    sequelize,
    modelName: 'Return',
  });
  return Return;
};