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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"SEMI_TRAILER",


"FULL_TRAILER",


"DOLLY",


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

      },

year: {
        type: DataTypes.INTEGER,
      
      

      },

first_registration_at: {
        type: DataTypes.DATE,
      
      

      },

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,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  trailers.associate = (db) => {


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


























    db.trailers.hasMany(db.vehicle_trailer_links, {
      as: 'vehicle_trailer_links_trailer',
      foreignKey: {
          name: 'trailerId',
      },
      constraints: false,
    });


    db.trailers.hasMany(db.vehicle_inspections, {
      as: 'vehicle_inspections_trailer',
      foreignKey: {
          name: 'trailerId',
      },
      constraints: false,
    });


    db.trailers.hasMany(db.vehicle_maintenances, {
      as: 'vehicle_maintenances_trailer',
      foreignKey: {
          name: 'trailerId',
      },
      constraints: false,
    });


    db.trailers.hasMany(db.vehicle_insurances, {
      as: 'vehicle_insurances_trailer',
      foreignKey: {
          name: 'trailerId',
      },
      constraints: false,
    });


    db.trailers.hasMany(db.vehicle_documents, {
      as: 'vehicle_documents_trailer',
      foreignKey: {
          name: 'trailerId',
      },
      constraints: false,
    });







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







    db.trailers.hasMany(db.cost_items, {
      as: 'cost_items_trailer',
      foreignKey: {
          name: 'trailerId',
      },
      constraints: false,
    });






//end loop



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



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


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

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



  return trailers;
};


