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

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"website",


"document",


"video",


"audio",


"note",


"dataset"

        ],

      },

source_name: {
        type: DataTypes.TEXT,
      
      

      },

source_url: {
        type: DataTypes.TEXT,
      
      

      },

trust_tier: {
        type: DataTypes.ENUM,
      
      

        values: [

"gold",


"silver",


"bronze",


"blocked"

        ],

      },

content_domain: {
        type: DataTypes.ENUM,
      
      

        values: [

"ndis",


"compliance",


"behavioral_science",


"platform_policy",


"competitor_research",


"brand_assets",


"campaign_reports",


"general_marketing"

        ],

      },

last_ingested_at: {
        type: DataTypes.DATE,
      
      

      },

next_refresh_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

refresh_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  knowledge_sources.associate = (db) => {


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











    db.knowledge_sources.hasMany(db.knowledge_documents, {
      as: 'knowledge_documents_knowledge_source',
      foreignKey: {
          name: 'knowledge_sourceId',
      },
      constraints: false,
    });

















    db.knowledge_sources.hasMany(db.refresh_jobs, {
      as: 'refresh_jobs_knowledge_source',
      foreignKey: {
          name: 'knowledge_sourceId',
      },
      constraints: false,
    });




//end loop





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


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

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



  return knowledge_sources;
};


