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

visit_date: {
        type: DataTypes.DATE,
      
      

      },

verification_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"site_visit",


"remote_validation",


"document_review",


"stakeholder_interview"

        ],

      },

outcome: {
        type: DataTypes.ENUM,
      
      

        values: [

"pass",


"partial",


"fail",


"follow_up_required"

        ],

      },

findings: {
        type: DataTypes.TEXT,
      
      

      },

actions_required: {
        type: DataTypes.TEXT,
      
      

      },

next_followup_at: {
        type: DataTypes.DATE,
      
      

      },

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

  field_verifications.associate = (db) => {


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




















































//end loop



    db.field_verifications.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.field_verifications.belongsTo(db.users, {
      as: 'verified_by_user',
      foreignKey: {
        name: 'verified_by_userId',
      },
      constraints: false,
    });

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



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


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

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



  return field_verifications;
};


