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

driver_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  driver_profiles.associate = (db) => {


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

















    db.driver_profiles.hasMany(db.driver_compliances, {
      as: 'driver_compliances_driver_profile',
      foreignKey: {
          name: 'driver_profileId',
      },
      constraints: false,
    });


    db.driver_profiles.hasMany(db.incidents, {
      as: 'incidents_driver_profile',
      foreignKey: {
          name: 'driver_profileId',
      },
      constraints: false,
    });


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





























//end loop



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

    db.driver_profiles.belongsTo(db.driver_compliances, {
      as: 'compliance',
      foreignKey: {
        name: 'complianceId',
      },
      constraints: false,
    });



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


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

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



  return driver_profiles;
};


