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

title_text: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

validation_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"valid",


"needs_review",


"rejected"

        ],

      },

ai_confidence: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  deep_dive_links.associate = (db) => {

    db.deep_dive_links.belongsToMany(db.citations, {
      as: 'citations',
      foreignKey: {
        name: 'deep_dive_links_citationsId',
      },
      constraints: false,
      through: 'deep_dive_linksCitationsCitations',
    });

    db.deep_dive_links.belongsToMany(db.citations, {
      as: 'citations_filter',
      foreignKey: {
        name: 'deep_dive_links_citationsId',
      },
      constraints: false,
      through: 'deep_dive_linksCitationsCitations',
    });


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






















    db.deep_dive_links.hasMany(db.posts, {
      as: 'posts_deep_dive_link',
      foreignKey: {
          name: 'deep_dive_linkId',
      },
      constraints: false,
    });























//end loop



    db.deep_dive_links.belongsTo(db.users, {
      as: 'creator',
      foreignKey: {
        name: 'creatorId',
      },
      constraints: false,
    });

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



    db.deep_dive_links.hasMany(db.file, {
      as: 'evidence_files',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.deep_dive_links.getTableName(),
        belongsToColumn: 'evidence_files',
      },
    });


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

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



  return deep_dive_links;
};


