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,
      
      

      },

sale_price_ht: {
        type: DataTypes.DECIMAL,
      
      

      },

purchase_price_ht: {
        type: DataTypes.DECIMAL,
      
      

      },

stock_on_hand: {
        type: DataTypes.DECIMAL,
      
      

      },

stock_reserved: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_level: {
        type: DataTypes.DECIMAL,
      
      

      },

weighted_avg_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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



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



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



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




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



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



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


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


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



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





//end loop



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

    db.products.belongsTo(db.product_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

    db.products.belongsTo(db.units, {
      as: 'unit',
      foreignKey: {
        name: 'unitId',
      },
      constraints: false,
    });

    db.products.belongsTo(db.tax_rates, {
      as: 'tax_rate',
      foreignKey: {
        name: 'tax_rateId',
      },
      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;
};


