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

patient_code: {
        type: DataTypes.TEXT,
      
      

      },

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

sex: {
        type: DataTypes.ENUM,
      
      

        values: [

"female",


"male",


"other",


"unknown"

        ],

      },

disease_stage: {
        type: DataTypes.ENUM,
      
      

        values: [

"mild",


"moderate",


"severe",


"unknown"

        ],

      },

diagnosis_date: {
        type: DataTypes.DATE,
      
      

      },

medical_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  patients.associate = (db) => {


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







    db.patients.hasMany(db.patient_access_grants, {
      as: 'patient_access_grants_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.medications, {
      as: 'medications_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.medication_schedules, {
      as: 'medication_schedules_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.calendar_events, {
      as: 'calendar_events_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.reminders, {
      as: 'reminders_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });




    db.patients.hasMany(db.cognitive_sessions, {
      as: 'cognitive_sessions_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });



    db.patients.hasMany(db.stage_assessments, {
      as: 'stage_assessments_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.voice_assistant_sessions, {
      as: 'voice_assistant_sessions_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.iot_devices, {
      as: 'iot_devices_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.iot_telemetry_events, {
      as: 'iot_telemetry_events_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.geofences, {
      as: 'geofences_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.alerts, {
      as: 'alerts_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });



    db.patients.hasMany(db.shared_dashboard_posts, {
      as: 'shared_dashboard_posts_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });



    db.patients.hasMany(db.article_recommendations, {
      as: 'article_recommendations_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.clinical_reports, {
      as: 'clinical_reports_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });




//end loop



    db.patients.belongsTo(db.accounts, {
      as: 'account',
      foreignKey: {
        name: 'accountId',
      },
      constraints: false,
    });

    db.patients.belongsTo(db.users, {
      as: 'primary_physician',
      foreignKey: {
        name: 'primary_physicianId',
      },
      constraints: false,
    });



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


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

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



  return patients;
};


