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

product_name_snapshot: {
        type: DataTypes.TEXT,
      
      

      },

sku_snapshot: {
        type: DataTypes.TEXT,
      
      

      },

barcode_snapshot: {
        type: DataTypes.TEXT,
      
      

      },

quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

unit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

line_discount: {
        type: DataTypes.DECIMAL,
      
      

      },

vat_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

vat_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

line_total: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  sale_items.associate = (db) => {


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




















    db.sale_items.hasMany(db.return_items, {
      as: 'return_items_sale_item',
      foreignKey: {
          name: 'sale_itemId',
      },
      constraints: false,
    });










//end loop



    db.sale_items.belongsTo(db.sales, {
      as: 'sale',
      foreignKey: {
        name: 'saleId',
      },
      constraints: false,
    });

    db.sale_items.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });

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




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

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



  return sale_items;
};


