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

report_date: {
        type: DataTypes.DATE,
      
      

      },

report_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"consultation",


"lab",


"imaging",


"follow_up",


"camp_visit"

        ],

      },

chief_complaint: {
        type: DataTypes.TEXT,
      
      

      },

symptoms: {
        type: DataTypes.TEXT,
      
      

      },

diagnosis: {
        type: DataTypes.TEXT,
      
      

      },

clinical_notes: {
        type: DataTypes.TEXT,
      
      

      },

prescription_notes: {
        type: DataTypes.TEXT,
      
      

      },

recommended_tests: {
        type: DataTypes.TEXT,
      
      

      },

is_finalized: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

finalized_at: {
        type: DataTypes.DATE,
      
      

      },

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

  medical_reports.associate = (db) => {


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






















    db.medical_reports.hasMany(db.medicine_reminders, {
      as: 'medicine_reminders_medical_report',
      foreignKey: {
          name: 'medical_reportId',
      },
      constraints: false,
    });






//end loop



    db.medical_reports.belongsTo(db.patients, {
      as: 'patient',
      foreignKey: {
        name: 'patientId',
      },
      constraints: false,
    });

    db.medical_reports.belongsTo(db.city_doctors, {
      as: 'author_city_doctor',
      foreignKey: {
        name: 'author_city_doctorId',
      },
      constraints: false,
    });

    db.medical_reports.belongsTo(db.rural_doctors, {
      as: 'author_rural_doctor',
      foreignKey: {
        name: 'author_rural_doctorId',
      },
      constraints: false,
    });



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

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


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

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



  return medical_reports;
};


