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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"TRUCK",


"TRACTOR",


"VAN",


"CAR",


"BUS",


"OTHER"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"ACTIVE",


"INACTIVE",


"SOLD",


"SCRAPPED",


"SUSPENDED"

        ],

      },

ownership: {
        type: DataTypes.ENUM,
      
      

        values: [

"OWNED",


"LEASED",


"RENTED"

        ],

      },

plate_number: {
        type: DataTypes.TEXT,
      
      

      },

vin: {
        type: DataTypes.TEXT,
      
      

      },

brand: {
        type: DataTypes.TEXT,
      
      

      },

model: {
        type: DataTypes.TEXT,
      
      

      },

variant: {
        type: DataTypes.TEXT,
      
      

      },

year: {
        type: DataTypes.INTEGER,
      
      

      },

first_registration_at: {
        type: DataTypes.DATE,
      
      

      },

fuel_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"DIESEL",


"LNG",


"CNG",


"ELECTRIC",


"HYBRID",


"OTHER"

        ],

      },

emission_class: {
        type: DataTypes.ENUM,
      
      

        values: [

"EURO_3",


"EURO_4",


"EURO_5",


"EURO_6",


"OTHER"

        ],

      },

gross_weight_kg: {
        type: DataTypes.INTEGER,
      
      

      },

curb_weight_kg: {
        type: DataTypes.INTEGER,
      
      

      },

payload_kg: {
        type: DataTypes.INTEGER,
      
      

      },

axle_count: {
        type: DataTypes.INTEGER,
      
      

      },

has_liftgate: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

tachograph_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"ANALOG",


"DIGITAL",


"SMART_1",


"SMART_2",


"OTHER"

        ],

      },

tachograph_serial: {
        type: DataTypes.TEXT,
      
      

      },

tachograph_last_calibration_at: {
        type: DataTypes.DATE,
      
      

      },

tachograph_next_calibration_at: {
        type: DataTypes.DATE,
      
      

      },

toll_provider: {
        type: DataTypes.ENUM,
      
      

        values: [

"TOLL_COLLECT",


"OTHER"

        ],

      },

toll_obu_serial: {
        type: DataTypes.TEXT,
      
      

      },

toll_contract_no: {
        type: DataTypes.TEXT,
      
      

      },

insurance_notes: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  vehicles.associate = (db) => {


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


























    db.vehicles.hasMany(db.vehicle_trailer_links, {
      as: 'vehicle_trailer_links_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });


    db.vehicles.hasMany(db.vehicle_inspections, {
      as: 'vehicle_inspections_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });


    db.vehicles.hasMany(db.vehicle_maintenances, {
      as: 'vehicle_maintenances_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });


    db.vehicles.hasMany(db.vehicle_insurances, {
      as: 'vehicle_insurances_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });


    db.vehicles.hasMany(db.vehicle_documents, {
      as: 'vehicle_documents_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });



    db.vehicles.hasMany(db.driver_vehicle_assignments, {
      as: 'driver_vehicle_assignments_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });





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







    db.vehicles.hasMany(db.cost_items, {
      as: 'cost_items_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });






//end loop



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



    db.vehicles.hasMany(db.file, {
      as: 'photo',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.vehicles.getTableName(),
        belongsToColumn: 'photo',
      },
    });


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

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



  return vehicles;
};


