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

medication_name: {
        type: DataTypes.TEXT,
      
      

      },

dose_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

dose_unit: {
        type: DataTypes.ENUM,
      
      

        values: [

"mg",


"ml",


"mcg",


"g",


"units",


"puffs",


"drops",


"sprays",


"patch",


"other"

        ],

      },

frequency_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"once_daily",


"twice_daily",


"three_times_daily",


"every_x_hours",


"weekly",


"monthly",


"as_needed",


"custom"

        ],

      },

frequency_interval_hours: {
        type: DataTypes.INTEGER,
      
      

      },

frequency_notes: {
        type: DataTypes.TEXT,
      
      

      },

route: {
        type: DataTypes.ENUM,
      
      

        values: [

"oral",


"topical",


"inhaled",


"injected",


"sublingual",


"nasal",


"rectal",


"other"

        ],

      },

condition_treated: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

refill_reminder_date: {
        type: DataTypes.DATE,
      
      

      },

reminders_enabled: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

is_controlled_substance: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  medications.associate = (db) => {


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
















    db.medications.hasMany(db.medication_doses, {
      as: 'medication_doses_medication',
      foreignKey: {
          name: 'medicationId',
      },
      constraints: false,
    });


    db.medications.hasMany(db.medication_side_effects, {
      as: 'medication_side_effects_medication',
      foreignKey: {
          name: 'medicationId',
      },
      constraints: false,
    });


































































































//end loop



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

    db.medications.belongsTo(db.healthcare_providers, {
      as: 'prescribing_provider',
      foreignKey: {
        name: 'prescribing_providerId',
      },
      constraints: false,
    });

    db.medications.belongsTo(db.pharmacies, {
      as: 'pharmacy',
      foreignKey: {
        name: 'pharmacyId',
      },
      constraints: false,
    });



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


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

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



  return medications;
};


