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

display_name: {
        type: DataTypes.TEXT,
      
      

      },

make: {
        type: DataTypes.TEXT,
      
      

      },

model: {
        type: DataTypes.TEXT,
      
      

      },

year_label: {
        type: DataTypes.TEXT,
      
      

      },

vin: {
        type: DataTypes.TEXT,
      
      

      },

license_plate: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"available",


"reserved",


"rented",


"maintenance",


"inactive"

        ],

      },

odometer_km: {
        type: DataTypes.INTEGER,
      
      

      },

fuel_policy: {
        type: DataTypes.ENUM,
      
      

        values: [

"full_to_full",


"same_to_same",


"prepurchase",


"free_tank"

        ],

      },

color: {
        type: DataTypes.TEXT,
      
      

      },

features: {
        type: DataTypes.TEXT,
      
      

      },

next_service_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

      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.bookings, {
      as: 'bookings_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });







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


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





//end loop



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

    db.vehicles.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.vehicles.belongsTo(db.vehicle_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

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



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


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

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



  return vehicles;
};


