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

item_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"product",


"service"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

sku: {
        type: DataTypes.TEXT,
      
      

      },

barcode: {
        type: DataTypes.TEXT,
      
      

      },

unit_name: {
        type: DataTypes.TEXT,
      
      

      },

sales_price: {
        type: DataTypes.DECIMAL,
      
      

      },

purchase_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

reorder_level: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

      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.inventory_items, {
      as: 'inventory_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



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



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













//end loop



    db.products.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.products.belongsTo(db.tax_codes, {
      as: 'tax_code',
      foreignKey: {
        name: 'tax_codeId',
      },
      constraints: false,
    });



    db.products.hasMany(db.file, {
      as: 'product_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.products.getTableName(),
        belongsToColumn: 'product_images',
      },
    });


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

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



  return products;
};


