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_title: {
        type: DataTypes.TEXT,
      
      

      },

document_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"gst_filing",


"tds_filing",


"license",


"certificate",


"roc_filing",


"policy",


"invoice",


"challan",


"other"

        ],

      },

file_name: {
        type: DataTypes.TEXT,
      
      

      },

mime_type: {
        type: DataTypes.TEXT,
      
      

      },

file_size_bytes: {
        type: DataTypes.INTEGER,
      
      

      },

version_label: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

tags: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        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_versions, {
      as: 'document_versions_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });
















//end loop



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

    db.documents.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.company_compliances, {
      as: 'related_compliance',
      foreignKey: {
        name: 'related_complianceId',
      },
      constraints: false,
    });

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

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



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


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

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



  return documents;
};


