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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"ID_CARD",


"PASSPORT",


"DRIVING_LICENSE",


"CPC_CODE95",


"ADR",


"MEDICAL",


"PSYCH",


"TACHO_CARD",


"WORK_CONTRACT",


"BANK_DETAILS",


"INCIDENT_REPORT",


"OTHER"

        ],

      },

scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"EMPLOYEE",


"DRIVER",


"DISPATCHER",


"ACCOUNTING"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  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.documents.hasMany(db.document_versions, {
      as: 'document_versions_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });




























//end loop



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

    db.documents.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.driver_profiles, {
      as: 'driver_profile',
      foreignKey: {
        name: 'driver_profileId',
      },
      constraints: false,
    });

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




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

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



  return documents;
};


