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

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"url",


"article",


"pdf",


"doc",


"ppt",


"text_note",


"spreadsheet"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

url: {
        type: DataTypes.TEXT,
      
      

      },

raw_text: {
        type: DataTypes.TEXT,
      
      

      },

extracted_summary: {
        type: DataTypes.TEXT,
      
      

      },

ingestion_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"processing",


"completed",


"failed"

        ],

      },

ingested_at: {
        type: DataTypes.DATE,
      
      

      },

tags: {
        type: DataTypes.TEXT,
      
      

      },

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

  source_items.associate = (db) => {


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













    db.source_items.hasMany(db.node_evidence_items, {
      as: 'node_evidence_items_source_item',
      foreignKey: {
          name: 'source_itemId',
      },
      constraints: false,
    });









//end loop



    db.source_items.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

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



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


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

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



  return source_items;
};


