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

patient_identifier: {
        type: DataTypes.TEXT,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"male",


"female",


"other",


"unknown"

        ],

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

blood_group: {
        type: DataTypes.TEXT,
      
      

      },

address_line: {
        type: DataTypes.TEXT,
      
      

      },

village: {
        type: DataTypes.TEXT,
      
      

      },

district: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

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

  patient_profiles.associate = (db) => {


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









    db.patient_profiles.hasMany(db.appointments, {
      as: 'appointments_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patient_profiles.hasMany(db.consultations, {
      as: 'consultations_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


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




    db.patient_profiles.hasMany(db.prescriptions, {
      as: 'prescriptions_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });



    db.patient_profiles.hasMany(db.medicine_dispatches, {
      as: 'medicine_dispatches_to_patient',
      foreignKey: {
          name: 'to_patientId',
      },
      constraints: false,
    });



    db.patient_profiles.hasMany(db.patient_vaccinations, {
      as: 'patient_vaccinations_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


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


    db.patient_profiles.hasMany(db.chat_sessions, {
      as: 'chat_sessions_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });




    db.patient_profiles.hasMany(db.chatbot_interactions, {
      as: 'chatbot_interactions_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patient_profiles.hasMany(db.feedbacks, {
      as: 'feedbacks_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });



//end loop



    db.patient_profiles.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.patient_profiles.belongsTo(db.medical_camps, {
      as: 'registered_at_camp',
      foreignKey: {
        name: 'registered_at_campId',
      },
      constraints: false,
    });

    db.patient_profiles.belongsTo(db.doctor_profiles, {
      as: 'primary_rural_doctor',
      foreignKey: {
        name: 'primary_rural_doctorId',
      },
      constraints: false,
    });

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



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


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

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



  return patient_profiles;
};


