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

prescribed_at: {
        type: DataTypes.DATE,
      
      

      },

prescription_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"issued",


"cancelled",


"fulfilled",


"partially_fulfilled"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  prescriptions.associate = (db) => {


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















    db.prescriptions.hasMany(db.prescription_items, {
      as: 'prescription_items_prescription',
      foreignKey: {
          name: 'prescriptionId',
      },
      constraints: false,
    });


    db.prescriptions.hasMany(db.medicine_dispatches, {
      as: 'medicine_dispatches_prescription',
      foreignKey: {
          name: 'prescriptionId',
      },
      constraints: false,
    });











//end loop



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

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

    db.prescriptions.belongsTo(db.doctor_profiles, {
      as: 'prescribed_by_doctor',
      foreignKey: {
        name: 'prescribed_by_doctorId',
      },
      constraints: false,
    });

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



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


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

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



  return prescriptions;
};


