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

name: {
        type: DataTypes.TEXT,
      
      

      },

sku: {
        type: DataTypes.TEXT,
      
      

      },

barcode: {
        type: DataTypes.TEXT,
      
      

      },

product_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"physical",


"service",


"digital"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

cost_price: {
        type: DataTypes.DECIMAL,
      
      

      },

sale_price: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_point: {
        type: DataTypes.INTEGER,
      
      

      },

is_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) => {

    db.products.belongsToMany(db.tags, {
      as: 'tags',
      foreignKey: {
        name: 'products_tagsId',
      },
      constraints: false,
      through: 'productsTagsTags',
    });

    db.products.belongsToMany(db.tags, {
      as: 'tags_filter',
      foreignKey: {
        name: 'products_tagsId',
      },
      constraints: false,
      through: 'productsTagsTags',
    });


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




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















//end loop



    db.products.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.products.belongsTo(db.business_units, {
      as: 'business_unit',
      foreignKey: {
        name: 'business_unitId',
      },
      constraints: false,
    });



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


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

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



  return products;
};


