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,
      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

dispatched_at: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"requested",


"approved",


"packed",


"dispatched",


"in_transit",


"delivered",


"cancelled"

        ],

      },

dispatch_note: {
        type: DataTypes.TEXT,
      
      

      },

destination_address: {
        type: DataTypes.TEXT,
      
      

      },

destination_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

destination_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

tracking_reference: {
        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













    db.medicine_dispatches.hasMany(db.dispatch_items, {
      as: 'dispatch_items_medicine_dispatch',
      foreignKey: {
          name: 'medicine_dispatchId',
      },
      constraints: false,
    });








    db.medicine_dispatches.hasMany(db.truck_tracking_events, {
      as: 'truck_tracking_events_medicine_dispatch',
      foreignKey: {
          name: 'medicine_dispatchId',
      },
      constraints: false,
    });








//end loop



    db.medicine_dispatches.belongsTo(db.rural_doctors, {
      as: 'requested_by_rural_doctor',
      foreignKey: {
        name: 'requested_by_rural_doctorId',
      },
      constraints: false,
    });

    db.medicine_dispatches.belongsTo(db.admins, {
      as: 'approved_by_admin',
      foreignKey: {
        name: 'approved_by_adminId',
      },
      constraints: false,
    });

    db.medicine_dispatches.belongsTo(db.city_doctors, {
      as: 'coordinated_by_city_doctor',
      foreignKey: {
        name: 'coordinated_by_city_doctorId',
      },
      constraints: false,
    });




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

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



  return medicine_dispatches;
};


