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,
      },

title: {
        type: DataTypes.TEXT,
      
      

      },

document_number: {
        type: DataTypes.TEXT,
      
      

      },

current_revision: {
        type: DataTypes.TEXT,
      
      

      },

lifecycle_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"approved",


"effective",


"superseded",


"obsolete",


"archived"

        ],

      },

control_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"controlled",


"uncontrolled"

        ],

      },

confidentiality: {
        type: DataTypes.ENUM,
      
      

        values: [

"public_internal",


"restricted",


"confidential",


"highly_confidential"

        ],

      },

effective_date: {
        type: DataTypes.DATE,
      
      

      },

next_review_date: {
        type: DataTypes.DATE,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

training_required: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  documents.associate = (db) => {

    db.documents.belongsToMany(db.clauses, {
      as: 'mapped_clauses',
      foreignKey: {
        name: 'documents_mapped_clausesId',
      },
      constraints: false,
      through: 'documentsMapped_clausesClauses',
    });

    db.documents.belongsToMany(db.clauses, {
      as: 'mapped_clauses_filter',
      foreignKey: {
        name: 'documents_mapped_clausesId',
      },
      constraints: false,
      through: 'documentsMapped_clausesClauses',
    });


/// 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,
    });


    db.documents.hasMany(db.change_requests, {
      as: 'change_requests_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });







    db.documents.hasMany(db.training_assignments, {
      as: 'training_assignments_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });




    db.documents.hasMany(db.batches, {
      as: 'batches_bmr_document',
      foreignKey: {
          name: 'bmr_documentId',
      },
      constraints: false,
    });






    db.documents.hasMany(db.capas, {
      as: 'capas_related_document',
      foreignKey: {
          name: 'related_documentId',
      },
      constraints: false,
    });










//end loop



    db.documents.belongsTo(db.document_types, {
      as: 'document_type',
      foreignKey: {
        name: 'document_typeId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.document_templates, {
      as: 'template',
      foreignKey: {
        name: 'templateId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.users, {
      as: 'approver',
      foreignKey: {
        name: 'approverId',
      },
      constraints: false,
    });



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


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

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



  return documents;
};


