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 ai_insights = sequelize.define(
    'ai_insights',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

insight_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"sales_forecast",


"stock_forecast",


"performance_drop",


"top_reseller",


"strategy_recommendation",


"market_opportunity"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"info",


"warning",


"critical"

        ],

      },

insight_title: {
        type: DataTypes.TEXT,
      
      

      },

insight_summary: {
        type: DataTypes.TEXT,
      
      

      },

recommendation: {
        type: DataTypes.TEXT,
      
      

      },

generated_at: {
        type: DataTypes.DATE,
      
      

      },

valid_until: {
        type: DataTypes.DATE,
      
      

      },

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

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  ai_insights.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity




























//end loop



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

    db.ai_insights.belongsTo(db.users, {
      as: 'related_user',
      foreignKey: {
        name: 'related_userId',
      },
      constraints: false,
    });

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

    db.ai_insights.belongsTo(db.warehouses, {
      as: 'related_warehouse',
      foreignKey: {
        name: 'related_warehouseId',
      },
      constraints: false,
    });

    db.ai_insights.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return ai_insights;
};


