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

document_number: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"approved",


"rejected",


"archived"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"normal",


"high",


"urgent"

        ],

      },

document_date: {
        type: DataTypes.DATE,
      
      

      },

review_due_date: {
        type: DataTypes.DATE,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

tags: {
        type: DataTypes.TEXT,
      
      

      },

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

  documents.associate = (db) => {


/// 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_links, {
      as: 'document_links_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });








//end loop



    db.documents.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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

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

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



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


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

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



  return documents;
};


