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

packet_name: {
        type: DataTypes.TEXT,
      
      

      },

packet_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"offboarding",


"privileged_access",


"mfa",


"access_review",


"service_account",


"exception",


"quarterly_summary"

        ],

      },

packet_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"ready",


"delivered",


"archived"

        ],

      },

readiness_score: {
        type: DataTypes.DECIMAL,
      
      

      },

generated_on: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

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

  proof_packets.associate = (db) => {

    db.proof_packets.belongsToMany(db.artifacts, {
      as: 'artifacts',
      foreignKey: {
        name: 'proof_packets_artifactsId',
      },
      constraints: false,
      through: 'proof_packetsArtifactsArtifacts',
    });

    db.proof_packets.belongsToMany(db.artifacts, {
      as: 'artifacts_filter',
      foreignKey: {
        name: 'proof_packets_artifactsId',
      },
      constraints: false,
      through: 'proof_packetsArtifactsArtifacts',
    });

    db.proof_packets.belongsToMany(db.control_requirements, {
      as: 'mapped_controls',
      foreignKey: {
        name: 'proof_packets_mapped_controlsId',
      },
      constraints: false,
      through: 'proof_packetsMapped_controlsControl_requirements',
    });

    db.proof_packets.belongsToMany(db.control_requirements, {
      as: 'mapped_controls_filter',
      foreignKey: {
        name: 'proof_packets_mapped_controlsId',
      },
      constraints: false,
      through: 'proof_packetsMapped_controlsControl_requirements',
    });


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















    db.proof_packets.hasMany(db.remediation_items, {
      as: 'remediation_items_linked_packet',
      foreignKey: {
          name: 'linked_packetId',
      },
      constraints: false,
    });











//end loop



    db.proof_packets.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.proof_packets.belongsTo(db.control_frameworks, {
      as: 'framework',
      foreignKey: {
        name: 'frameworkId',
      },
      constraints: false,
    });

    db.proof_packets.belongsTo(db.identity_workflows, {
      as: 'workflow',
      foreignKey: {
        name: 'workflowId',
      },
      constraints: false,
    });

    db.proof_packets.belongsTo(db.sampled_subjects, {
      as: 'sampled_subject',
      foreignKey: {
        name: 'sampled_subjectId',
      },
      constraints: false,
    });

    db.proof_packets.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });




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

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



  return proof_packets;
};


