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

title: {
        type: DataTypes.TEXT,
      
      

      },

matter_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"civil",


"criminal",


"family",


"employment",


"housing",


"small_claims",


"personal_injury",


"medical_malpractice",


"contract",


"administrative",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"intake",


"active",


"stayed",


"closed",


"archived"

        ],

      },

jurisdiction: {
        type: DataTypes.TEXT,
      
      

      },

court_name: {
        type: DataTypes.TEXT,
      
      

      },

case_number: {
        type: DataTypes.TEXT,
      
      

      },

filed_at: {
        type: DataTypes.DATE,
      
      

      },

next_hearing_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  matters.associate = (db) => {


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








    db.matters.hasMany(db.matter_parties, {
      as: 'matter_parties_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });


    db.matters.hasMany(db.files, {
      as: 'files_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });



    db.matters.hasMany(db.assistant_threads, {
      as: 'assistant_threads_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });



    db.matters.hasMany(db.ai_jobs, {
      as: 'ai_jobs_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });




    db.matters.hasMany(db.generated_documents, {
      as: 'generated_documents_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });



    db.matters.hasMany(db.research_queries, {
      as: 'research_queries_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });




    db.matters.hasMany(db.citator_checks, {
      as: 'citator_checks_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });


    db.matters.hasMany(db.mock_trials, {
      as: 'mock_trials_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });




    db.matters.hasMany(db.transcription_jobs, {
      as: 'transcription_jobs_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });


    db.matters.hasMany(db.medical_chronology_reports, {
      as: 'medical_chronology_reports_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });



    db.matters.hasMany(db.causation_analyses, {
      as: 'causation_analyses_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });


    db.matters.hasMany(db.audit_events, {
      as: 'audit_events_matter',
      foreignKey: {
          name: 'matterId',
      },
      constraints: false,
    });



//end loop



    db.matters.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });




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

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



  return matters;
};


