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,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"stocked",


"service",


"non_stocked"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

standard_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

default_price: {
        type: DataTypes.DECIMAL,
      
      

      },

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


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



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



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



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



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



    db.products.hasMany(db.shipment_lines, {
      as: 'shipment_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.categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

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

    db.products.belongsTo(db.taxes, {
      as: 'tax',
      foreignKey: {
        name: 'taxId',
      },
      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;
};


