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

dispatched_at: {
        type: DataTypes.DATE,
      
      

      },

expected_delivery_at: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

dispatch_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"created",


"packed",


"in_transit",


"delivered",


"failed",


"returned",


"cancelled"

        ],

      },

tracking_reference: {
        type: DataTypes.TEXT,
      
      

      },

last_known_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

last_known_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

delivery_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  medicine_dispatches.associate = (db) => {


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


























//end loop



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

    db.medicine_dispatches.belongsTo(db.organizations, {
      as: 'from_organization',
      foreignKey: {
        name: 'from_organizationId',
      },
      constraints: false,
    });

    db.medicine_dispatches.belongsTo(db.patient_profiles, {
      as: 'to_patient',
      foreignKey: {
        name: 'to_patientId',
      },
      constraints: false,
    });

    db.medicine_dispatches.belongsTo(db.organizations, {
      as: 'to_organization',
      foreignKey: {
        name: 'to_organizationId',
      },
      constraints: false,
    });

    db.medicine_dispatches.belongsTo(db.doctor_profiles, {
      as: 'dispatched_by_doctor',
      foreignKey: {
        name: 'dispatched_by_doctorId',
      },
      constraints: false,
    });




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

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



  return medicine_dispatches;
};


