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

trip_no: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"PLANNED",


"DISPATCHED",


"IN_PROGRESS",


"COMPLETED",


"CANCELLED"

        ],

      },

planned_start_at: {
        type: DataTypes.DATE,
      
      

      },

planned_end_at: {
        type: DataTypes.DATE,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

start_odometer_km: {
        type: DataTypes.INTEGER,
      
      

      },

end_odometer_km: {
        type: DataTypes.INTEGER,
      
      

      },

distance_km: {
        type: DataTypes.INTEGER,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  trips.associate = (db) => {


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





































    db.trips.hasMany(db.trip_loads, {
      as: 'trip_loads_trip',
      foreignKey: {
          name: 'tripId',
      },
      constraints: false,
    });




    db.trips.hasMany(db.invoice_lines, {
      as: 'invoice_lines_trip',
      foreignKey: {
          name: 'tripId',
      },
      constraints: false,
    });



    db.trips.hasMany(db.cost_items, {
      as: 'cost_items_trip',
      foreignKey: {
          name: 'tripId',
      },
      constraints: false,
    });






//end loop



    db.trips.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.trips.belongsTo(db.users, {
      as: 'dispatcher_user',
      foreignKey: {
        name: 'dispatcher_userId',
      },
      constraints: false,
    });

    db.trips.belongsTo(db.employees, {
      as: 'driver',
      foreignKey: {
        name: 'driverId',
      },
      constraints: false,
    });

    db.trips.belongsTo(db.vehicles, {
      as: 'vehicle',
      foreignKey: {
        name: 'vehicleId',
      },
      constraints: false,
    });

    db.trips.belongsTo(db.trailers, {
      as: 'trailer',
      foreignKey: {
        name: 'trailerId',
      },
      constraints: false,
    });




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

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



  return trips;
};


