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,
      },

number: {
        type: DataTypes.TEXT,
      
      

      },

sale_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"confirmed",


"delivered",


"cancelled"

        ],

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_total: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

payment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unpaid",


"partial",


"paid",


"refunded"

        ],

      },

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.invoices, {
      as: 'invoices_sale',
      foreignKey: {
          name: 'saleId',
      },
      constraints: false,
    });













//end loop



    db.sales.belongsTo(db.clients, {
      as: 'client',
      foreignKey: {
        name: 'clientId',
      },
      constraints: false,
    });

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




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

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



  return sales;
};


