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

artifact_name: {
        type: DataTypes.TEXT,
      
      

      },

artifact_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"screenshot",


"export_csv",


"export_json",


"audit_log",


"ticket",


"attestation",


"configuration",


"policy_document",


"api_snapshot",


"other"

        ],

      },

artifact_reference: {
        type: DataTypes.TEXT,
      
      

      },

collected_at: {
        type: DataTypes.DATE,
      
      

      },

event_timestamp: {
        type: DataTypes.DATE,
      
      

      },

subject_user: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"collected",


"validated",


"rejected",


"expired"

        ],

      },

validation_notes: {
        type: DataTypes.TEXT,
      
      

      },

evidence_strength_score: {
        type: DataTypes.DECIMAL,
      
      

      },

chain_integrity_score: {
        type: DataTypes.DECIMAL,
      
      

      },

retention_expiry: {
        type: DataTypes.DATE,
      
      

      },

tags: {
        type: DataTypes.TEXT,
      
      

      },

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

  artifacts.associate = (db) => {


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















    db.artifacts.hasMany(db.remediation_items, {
      as: 'remediation_items_linked_artifact',
      foreignKey: {
          name: 'linked_artifactId',
      },
      constraints: false,
    });











//end loop



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

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

    db.artifacts.belongsTo(db.evidence_models, {
      as: 'evidence_model',
      foreignKey: {
        name: 'evidence_modelId',
      },
      constraints: false,
    });

    db.artifacts.belongsTo(db.connected_systems, {
      as: 'source_system',
      foreignKey: {
        name: 'source_systemId',
      },
      constraints: false,
    });

    db.artifacts.belongsTo(db.users, {
      as: 'collected_by_user',
      foreignKey: {
        name: 'collected_by_userId',
      },
      constraints: false,
    });

    db.artifacts.belongsTo(db.users, {
      as: 'approver_user',
      foreignKey: {
        name: 'approver_userId',
      },
      constraints: false,
    });



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


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

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



  return artifacts;
};


