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,
      
      

      },

sold_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"completed",


"voided",


"refunded",


"partially_refunded"

        ],

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_total: {
        type: DataTypes.DECIMAL,
      
      

      },

vat_total: {
        type: DataTypes.DECIMAL,
      
      

      },

grand_total: {
        type: DataTypes.DECIMAL,
      
      

      },

amount_paid: {
        type: DataTypes.DECIMAL,
      
      

      },

change_given: {
        type: DataTypes.DECIMAL,
      
      

      },

receipt_delivery: {
        type: DataTypes.ENUM,
      
      

        values: [

"print",


"email",


"sms",


"none"

        ],

      },

customer_email: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        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_items, {
      as: 'sale_items_sale',
      foreignKey: {
          name: 'saleId',
      },
      constraints: false,
    });


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


    db.sales.hasMany(db.returns, {
      as: 'returns_original_sale',
      foreignKey: {
          name: 'original_saleId',
      },
      constraints: false,
    });











//end loop



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

    db.sales.belongsTo(db.registers, {
      as: 'register',
      foreignKey: {
        name: 'registerId',
      },
      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.cash_drawer_sessions, {
      as: 'cash_drawer_session',
      foreignKey: {
        name: 'cash_drawer_sessionId',
      },
      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;
};


