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

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"case",


"statute",


"rule",


"regulation"

        ],

      },

jurisdiction: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

citation: {
        type: DataTypes.TEXT,
      
      

      },

decision_date: {
        type: DataTypes.DATE,
      
      

      },

court: {
        type: DataTypes.TEXT,
      
      

      },

full_text: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

source_url: {
        type: DataTypes.TEXT,
      
      

      },

last_updated_at: {
        type: DataTypes.DATE,
      
      

      },

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

  legal_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.legal_sources.hasMany(db.research_results, {
      as: 'research_results_legal_source',
      foreignKey: {
          name: 'legal_sourceId',
      },
      constraints: false,
    });


    db.legal_sources.hasMany(db.citator_checks, {
      as: 'citator_checks_legal_source',
      foreignKey: {
          name: 'legal_sourceId',
      },
      constraints: false,
    });











//end loop






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

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



  return legal_sources;
};


