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

scheduled_start_at: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end_at: {
        type: DataTypes.DATE,
      
      

      },

appointment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"in_person",


"chat",


"video",


"phone"

        ],

      },

appointment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"requested",


"confirmed",


"in_progress",


"completed",


"cancelled",


"no_show"

        ],

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  appointments.associate = (db) => {


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










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

















//end loop



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

    db.appointments.belongsTo(db.doctor_profiles, {
      as: 'doctor',
      foreignKey: {
        name: 'doctorId',
      },
      constraints: false,
    });

    db.appointments.belongsTo(db.medical_camps, {
      as: 'camp',
      foreignKey: {
        name: 'campId',
      },
      constraints: false,
    });

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




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

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



  return appointments;
};


