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

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"in_person",


"chat",


"video",


"phone"

        ],

      },

consultation_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"closed",


"cancelled"

        ],

      },

chief_complaint: {
        type: DataTypes.TEXT,
      
      

      },

clinical_notes: {
        type: DataTypes.TEXT,
      
      

      },

diagnosis_summary: {
        type: DataTypes.TEXT,
      
      

      },

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

  consultations.associate = (db) => {


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











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




    db.consultations.hasMany(db.prescriptions, {
      as: 'prescriptions_consultation',
      foreignKey: {
          name: 'consultationId',
      },
      constraints: false,
    });









    db.consultations.hasMany(db.video_calls, {
      as: 'video_calls_consultation',
      foreignKey: {
          name: 'consultationId',
      },
      constraints: false,
    });



    db.consultations.hasMany(db.feedbacks, {
      as: 'feedbacks_consultation',
      foreignKey: {
          name: 'consultationId',
      },
      constraints: false,
    });



//end loop



    db.consultations.belongsTo(db.appointments, {
      as: 'appointment',
      foreignKey: {
        name: 'appointmentId',
      },
      constraints: false,
    });

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

    db.consultations.belongsTo(db.doctor_profiles, {
      as: 'primary_doctor',
      foreignKey: {
        name: 'primary_doctorId',
      },
      constraints: false,
    });

    db.consultations.belongsTo(db.doctor_profiles, {
      as: 'secondary_doctor',
      foreignKey: {
        name: 'secondary_doctorId',
      },
      constraints: false,
    });

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



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


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

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



  return consultations;
};


