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 = sequelize.define(
    'sales',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

receipt_number: {
        type: DataTypes.TEXT,
      
      

      },

sale_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"card",


"mobile_money",


"credit",


"split"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"completed",


"voided",


"refunded",


"parked"

        ],

      },

sold_at: {
        type: DataTypes.DATE,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_total: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

amount_paid: {
        type: DataTypes.DECIMAL,
      
      

      },

change_due: {
        type: DataTypes.DECIMAL,
      
      

      },

void_reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  sales.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.hasMany(db.sale_lines, {
      as: 'sale_lines_sale',
      foreignKey: {
          name: 'saleId',
      },
      constraints: false,
    });


    db.sales.hasMany(db.payments, {
      as: 'payments_sale',
      foreignKey: {
          name: 'saleId',
      },
      constraints: false,
    });


    db.sales.hasMany(db.refunds, {
      as: 'refunds_sale',
      foreignKey: {
          name: 'saleId',
      },
      constraints: false,
    });
























//end loop



    db.sales.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

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

    db.sales.belongsTo(db.pos_shifts, {
      as: 'shift',
      foreignKey: {
        name: 'shiftId',
      },
      constraints: false,
    });

    db.sales.belongsTo(db.users, {
      as: 'cashier',
      foreignKey: {
        name: 'cashierId',
      },
      constraints: false,
    });

    db.sales.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

    db.sales.belongsTo(db.users, {
      as: 'discount_approved_by',
      foreignKey: {
        name: 'discount_approved_byId',
      },
      constraints: false,
    });

    db.sales.belongsTo(db.users, {
      as: 'voided_by',
      foreignKey: {
        name: 'voided_byId',
      },
      constraints: false,
    });

    db.sales.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return sales;
};


