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_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"visit_summary",


"lab_result",


"imaging",


"prescription",


"vaccination_card",


"discharge_summary",


"other"

        ],

      },

report_date: {
        type: DataTypes.DATE,
      
      

      },

report_title: {
        type: DataTypes.TEXT,
      
      

      },

report_notes: {
        type: DataTypes.TEXT,
      
      

      },

qr_payload: {
        type: DataTypes.TEXT,
      
      

      },

      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


























//end loop



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

    db.medical_reports.belongsTo(db.consultations, {
      as: 'consultation',
      foreignKey: {
        name: 'consultationId',
      },
      constraints: false,
    });

    db.medical_reports.belongsTo(db.doctor_profiles, {
      as: 'author_doctor',
      foreignKey: {
        name: 'author_doctorId',
      },
      constraints: false,
    });

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



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


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

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



  return medical_reports;
};


