const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const sales_transactions = sequelize.define(
    'sales_transactions',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

sold_at: {
        type: DataTypes.DATE,
      
      

      },

payment_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"pix",


"credit_card",


"debit_card"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"paid",


"voided",


"refunded"

        ],

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  sales_transactions.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
































    db.sales_transactions.hasMany(db.sales_transaction_lines, {
      as: 'sales_transaction_lines_sales_transaction',
      foreignKey: {
          name: 'sales_transactionId',
      },
      constraints: false,
    });




















//end loop



    db.sales_transactions.belongsTo(db.market_stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.sales_transactions.belongsTo(db.cash_registers, {
      as: 'register',
      foreignKey: {
        name: 'registerId',
      },
      constraints: false,
    });

    db.sales_transactions.belongsTo(db.employees, {
      as: 'cashier_employee',
      foreignKey: {
        name: 'cashier_employeeId',
      },
      constraints: false,
    });

    db.sales_transactions.belongsTo(db.npcs, {
      as: 'customer_npc',
      foreignKey: {
        name: 'customer_npcId',
      },
      constraints: false,
    });




    db.sales_transactions.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.sales_transactions.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return sales_transactions;
};


