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

truck_code: {
        type: DataTypes.TEXT,
      
      

      },

registration_number: {
        type: DataTypes.TEXT,
      
      

      },

driver_name: {
        type: DataTypes.TEXT,
      
      

      },

driver_phone: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"available",


"assigned",


"in_transit",


"maintenance",


"offline"

        ],

      },

last_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

last_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

last_ping_at: {
        type: DataTypes.DATE,
      
      

      },

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

  trucks.associate = (db) => {


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




















    db.trucks.hasMany(db.truck_tracking_events, {
      as: 'truck_tracking_events_truck',
      foreignKey: {
          name: 'truckId',
      },
      constraints: false,
    });








//end loop






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

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



  return trucks;
};


