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

product_name: {
        type: DataTypes.TEXT,
      
      

      },

sku: {
        type: DataTypes.TEXT,
      
      

      },

barcode: {
        type: DataTypes.TEXT,
      
      

      },

product_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"archived"

        ],

      },

brand: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.TEXT,
      
      

      },

unit: {
        type: DataTypes.TEXT,
      
      

      },

weight_grams: {
        type: DataTypes.DECIMAL,
      
      

      },

length_cm: {
        type: DataTypes.DECIMAL,
      
      

      },

width_cm: {
        type: DataTypes.DECIMAL,
      
      

      },

height_cm: {
        type: DataTypes.DECIMAL,
      
      

      },

cost_price: {
        type: DataTypes.DECIMAL,
      
      

      },

retail_price: {
        type: DataTypes.DECIMAL,
      
      

      },

reseller_price: {
        type: DataTypes.DECIMAL,
      
      

      },

agent_price: {
        type: DataTypes.DECIMAL,
      
      

      },

distributor_price: {
        type: DataTypes.DECIMAL,
      
      

      },

low_stock_threshold: {
        type: DataTypes.INTEGER,
      
      

      },

short_description: {
        type: DataTypes.TEXT,
      
      

      },

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




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





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





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





    db.products.hasMany(db.ai_insights, {
      as: 'ai_insights_related_product',
      foreignKey: {
          name: 'related_productId',
      },
      constraints: false,
    });




//end loop



    db.products.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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


