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_uid: {
        type: DataTypes.TEXT,
      
      

      },

full_name: {
        type: DataTypes.TEXT,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"male",


"female",


"other",


"prefer_not_to_say"

        ],

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

blood_group: {
        type: DataTypes.TEXT,
      
      

      },

allergies: {
        type: DataTypes.TEXT,
      
      

      },

chronic_conditions: {
        type: DataTypes.TEXT,
      
      

      },

emergency_contact_name: {
        type: DataTypes.TEXT,
      
      

      },

emergency_contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

registered_at: {
        type: DataTypes.DATE,
      
      

      },

account_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive",


"blocked"

        ],

      },

      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.medical_reports, {
      as: 'medical_reports_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });







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







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


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


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





//end loop



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

    db.patients.belongsTo(db.rural_doctors, {
      as: 'registered_by_rural_doctor',
      foreignKey: {
        name: 'registered_by_rural_doctorId',
      },
      constraints: false,
    });




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

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



  return patients;
};


