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

delivery_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending_assignment",


"assigned",


"picked_up",


"in_transit",


"delivered",


"failed",


"cancelled"

        ],

      },

delivery_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"walking",


"bicycle",


"motorbike",


"car",


"van"

        ],

      },

pickup_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

pickup_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

dropoff_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

dropoff_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

distance_km: {
        type: DataTypes.DECIMAL,
      
      

      },

courier_earnings: {
        type: DataTypes.DECIMAL,
      
      

      },

platform_commission: {
        type: DataTypes.DECIMAL,
      
      

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

picked_up_at: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

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

  deliveries.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.deliveries.belongsTo(db.orders, {
      as: 'order',
      foreignKey: {
        name: 'orderId',
      },
      constraints: false,
    });

    db.deliveries.belongsTo(db.users, {
      as: 'courier',
      foreignKey: {
        name: 'courierId',
      },
      constraints: false,
    });

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




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

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



  return deliveries;
};


