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

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pdf",


"document",


"note",


"link",


"webpage",


"image"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

source_url: {
        type: DataTypes.TEXT,
      
      

      },

ingestion_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"processing",


"ready",


"failed"

        ],

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

ingested_at: {
        type: DataTypes.DATE,
      
      

      },

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

  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.sources.hasMany(db.source_chunks, {
      as: 'source_chunks_source',
      foreignKey: {
          name: 'sourceId',
      },
      constraints: false,
    });


    db.sources.hasMany(db.ai_jobs, {
      as: 'ai_jobs_source',
      foreignKey: {
          name: 'sourceId',
      },
      constraints: false,
    });


    db.sources.hasMany(db.concept_maps, {
      as: 'concept_maps_source',
      foreignKey: {
          name: 'sourceId',
      },
      constraints: false,
    });


    db.sources.hasMany(db.study_guides, {
      as: 'study_guides_source',
      foreignKey: {
          name: 'sourceId',
      },
      constraints: false,
    });


    db.sources.hasMany(db.flashcard_decks, {
      as: 'flashcard_decks_source',
      foreignKey: {
          name: 'sourceId',
      },
      constraints: false,
    });






    db.sources.hasMany(db.quizzes, {
      as: 'quizzes_source',
      foreignKey: {
          name: 'sourceId',
      },
      constraints: false,
    });










//end loop



    db.sources.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.sources.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.sources.belongsTo(db.topics, {
      as: 'topic',
      foreignKey: {
        name: 'topicId',
      },
      constraints: false,
    });

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



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


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

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



  return sources;
};


