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

report_title: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"generated",


"exported"

        ],

      },

overview: {
        type: DataTypes.TEXT,
      
      

      },

generated_at: {
        type: DataTypes.DATE,
      
      

      },

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

  medical_chronology_reports.associate = (db) => {

    db.medical_chronology_reports.belongsToMany(db.files, {
      as: 'source_files',
      foreignKey: {
        name: 'medical_chronology_reports_source_filesId',
      },
      constraints: false,
      through: 'medical_chronology_reportsSource_filesFiles',
    });

    db.medical_chronology_reports.belongsToMany(db.files, {
      as: 'source_files_filter',
      foreignKey: {
        name: 'medical_chronology_reports_source_filesId',
      },
      constraints: false,
      through: 'medical_chronology_reportsSource_filesFiles',
    });


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



























    db.medical_chronology_reports.hasMany(db.medical_events, {
      as: 'medical_events_report',
      foreignKey: {
          name: 'reportId',
      },
      constraints: false,
    });





//end loop



    db.medical_chronology_reports.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

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

    db.medical_chronology_reports.belongsTo(db.files, {
      as: 'export_file',
      foreignKey: {
        name: 'export_fileId',
      },
      constraints: false,
    });




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

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



  return medical_chronology_reports;
};


