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

sku: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"frame_panel",


"truss",


"wall_frame",


"floor_frame",


"roof_frame",


"service",


"misc"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

unit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  products.associate = (db) => {


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









    db.products.hasMany(db.quote_line_items, {
      as: 'quote_line_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });










    db.products.hasMany(db.boms, {
      as: 'boms_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });






    db.products.hasMany(db.work_orders, {
      as: 'work_orders_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });




    db.products.hasMany(db.delivery_items, {
      as: 'delivery_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.invoice_line_items, {
      as: 'invoice_line_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });





//end loop






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

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



  return products;
};


