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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"REGISTRATION_CERT_PART_1",


"REGISTRATION_CERT_PART_2",


"INSURANCE_POLICY",


"LEASING_CONTRACT",


"HU_REPORT",


"AU_REPORT",


"UVV_REPORT",


"ADR_CERT",


"CMR_EQUIPMENT_LIST",


"TACHOGRAPH_CERT",


"OTHER"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  vehicle_documents.associate = (db) => {


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































    db.vehicle_documents.hasMany(db.vehicle_document_versions, {
      as: 'vehicle_document_versions_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });

















//end loop



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

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

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

    db.vehicle_documents.belongsTo(db.vehicle_document_versions, {
      as: 'current_version',
      foreignKey: {
        name: 'current_versionId',
      },
      constraints: false,
    });




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

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



  return vehicle_documents;
};


