'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Transaction 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
      Transaction.hasMany(models.TransactionItem, { foreignKey: 'transactionId', as: 'transactionsitems' });

       Transaction.hasMany(models.TransactionPayment, { foreignKey: 'transactionId', as: 'transactionpayment' });

        Transaction.belongsTo(models.User, { foreignKey: 'cashierId', as: 'cashier' });

        Transaction.belongsTo(models.User, { foreignKey: 'voidedBy', as: 'user' });

        Transaction.belongsTo(models.Store, { foreignKey: 'storeId', as: 'store' });

        Transaction.belongsTo(models.Register, { foreignKey: 'registerId', as: 'register' });


        Transaction.belongsTo(models.Customer, { foreignKey: 'customerId', as: 'customer' });

        Transaction.hasOne(models.Return, { foreignKey: 'transactionId', as: 'return' });

        

        
     
    
    }
  }
  Transaction.init({
    storeId: DataTypes.INTEGER,
    registerId: DataTypes.INTEGER,
    transactionNumber: DataTypes.STRING,
    customerId: DataTypes.INTEGER,
    cashierId: DataTypes.INTEGER,
    transactionDate: DataTypes.DATE,
    status: DataTypes.STRING,
    subtotal: DataTypes.DECIMAL,
    discountAmount: DataTypes.DECIMAL,
    taxAmount: DataTypes.DECIMAL,
    totalAmount: DataTypes.DECIMAL,
    voidedBy: DataTypes.INTEGER,
    voidedAt: DataTypes.DATE,
    voidReason: DataTypes.TEXT,
    isSuspended: DataTypes.BOOLEAN,
    notes: DataTypes.TEXT,
    suspendNote: DataTypes.TEXT,
    suspendedAt:DataTypes.DATE,
    originalTransactionId: DataTypes.INTEGER,
    resumedAt: DataTypes.DATE,
    isVoided: DataTypes.BOOLEAN,
    paidAt: DataTypes.DATE,
    finalizedAt: DataTypes.DATE,
    completedAt: DataTypes.DATE
  }, {
    sequelize,
    modelName: 'Transaction',
  });
  return Transaction;
};