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

      plate_number: {
        type: DataTypes.TEXT,
      },

      brand: {
        type: DataTypes.TEXT,
      },

      model: {
        type: DataTypes.TEXT,
      },

      year: {
        type: DataTypes.INTEGER,
      },

      color: {
        type: DataTypes.TEXT,
      },

      category: {
        type: DataTypes.ENUM,

        values: ['citadine', 'berline', 'SUV', 'utilitaire', 'luxe'],
      },

      fuel_type: {
        type: DataTypes.ENUM,

        values: ['essence', 'diesel', 'hybride', 'electrique'],
      },

      transmission: {
        type: DataTypes.ENUM,

        values: ['manuelle', 'automatique'],
      },

      mileage: {
        type: DataTypes.INTEGER,
      },

      daily_price: {
        type: DataTypes.DECIMAL,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['disponible', 'reserve', 'en_location', 'hors_service'],
      },

      insurance_expiry: {
        type: DataTypes.DATE,
      },

      tech_visit_expiry: {
        type: DataTypes.DATE,
      },

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

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

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

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

    //end loop

    db.vehicles.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

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

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

  return vehicles;
};
