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

visit_number: {
        type: DataTypes.TEXT,
      
      

      },

department: {
        type: DataTypes.ENUM,
      
      

        values: [

"reception",


"emergency",


"clinic",


"surgery",


"maternity"

        ],

      },

visit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

arrival_time: {
        type: DataTypes.DATE,
      
      

      },

discharge_time: {
        type: DataTypes.DATE,
      
      

      },

blood_pressure: {
        type: DataTypes.TEXT,
      
      

      },

glucose: {
        type: DataTypes.DECIMAL,
      
      

      },

pulse: {
        type: DataTypes.DECIMAL,
      
      

      },

temperature: {
        type: DataTypes.DECIMAL,
      
      

      },

spo2: {
        type: DataTypes.DECIMAL,
      
      

      },

chief_complaint: {
        type: DataTypes.TEXT,
      
      

      },

initial_diagnosis: {
        type: DataTypes.TEXT,
      
      

      },

emergency_treatment: {
        type: DataTypes.TEXT,
      
      

      },

fluids_iv: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"in_progress",


"completed",


"admitted",


"cancelled"

        ],

      },

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

  visits.associate = (db) => {


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









    db.visits.hasMany(db.orders, {
      as: 'orders_visit',
      foreignKey: {
          name: 'visitId',
      },
      constraints: false,
    });









    db.visits.hasMany(db.endoscopy_cases, {
      as: 'endoscopy_cases_visit',
      foreignKey: {
          name: 'visitId',
      },
      constraints: false,
    });






























//end loop



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

    db.visits.belongsTo(db.guarantors, {
      as: 'guarantor',
      foreignKey: {
        name: 'guarantorId',
      },
      constraints: false,
    });

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




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

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



  return visits;
};


