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,
      
      

      },

product_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"live_quail",


"eggs",


"meat",


"breeding_supply",


"feed",


"service",


"other"

        ],

      },

unit: {
        type: DataTypes.ENUM,
      
      

        values: [

"piece",


"kg",


"g",


"tray",


"box",


"liter",


"bag",


"service"

        ],

      },

sale_price: {
        type: DataTypes.DECIMAL,
      
      

      },

purchase_price: {
        type: DataTypes.DECIMAL,
      
      

      },

vat_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

low_stock_threshold: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

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



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



    db.products.hasMany(db.sale_items, {
      as: 'sale_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.alerts, {
      as: 'alerts_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });





//end loop



    db.products.belongsTo(db.categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      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;
};


