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

file_name: {
        type: DataTypes.TEXT,
      
      

      },

content_type: {
        type: DataTypes.TEXT,
      
      

      },

file_extension: {
        type: DataTypes.TEXT,
      
      

      },

size_bytes: {
        type: DataTypes.INTEGER,
      
      

      },

storage_provider: {
        type: DataTypes.TEXT,
      
      

      },

storage_key: {
        type: DataTypes.TEXT,
      
      

      },

checksum_sha256: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

processing_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"processing",


"completed",


"failed"

        ],

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

tags: {
        type: DataTypes.TEXT,
      
      

      },

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

  files.associate = (db) => {


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










    db.files.hasMany(db.file_extractions, {
      as: 'file_extractions_file',
      foreignKey: {
          name: 'fileId',
      },
      constraints: false,
    });




    db.files.hasMany(db.ai_jobs, {
      as: 'ai_jobs_file',
      foreignKey: {
          name: 'fileId',
      },
      constraints: false,
    });


    db.files.hasMany(db.legal_templates, {
      as: 'legal_templates_source_file',
      foreignKey: {
          name: 'source_fileId',
      },
      constraints: false,
    });



    db.files.hasMany(db.generated_documents, {
      as: 'generated_documents_export_file',
      foreignKey: {
          name: 'export_fileId',
      },
      constraints: false,
    });









    db.files.hasMany(db.mock_trial_turns, {
      as: 'mock_trial_turns_audio_file',
      foreignKey: {
          name: 'audio_fileId',
      },
      constraints: false,
    });


    db.files.hasMany(db.transcription_jobs, {
      as: 'transcription_jobs_media_file',
      foreignKey: {
          name: 'media_fileId',
      },
      constraints: false,
    });

    db.files.hasMany(db.transcription_jobs, {
      as: 'transcription_jobs_export_file',
      foreignKey: {
          name: 'export_fileId',
      },
      constraints: false,
    });


    db.files.hasMany(db.medical_chronology_reports, {
      as: 'medical_chronology_reports_export_file',
      foreignKey: {
          name: 'export_fileId',
      },
      constraints: false,
    });


    db.files.hasMany(db.medical_events, {
      as: 'medical_events_linked_file',
      foreignKey: {
          name: 'linked_fileId',
      },
      constraints: false,
    });


    db.files.hasMany(db.causation_analyses, {
      as: 'causation_analyses_export_file',
      foreignKey: {
          name: 'export_fileId',
      },
      constraints: false,
    });




//end loop



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

    db.files.belongsTo(db.matters, {
      as: 'matter',
      foreignKey: {
        name: 'matterId',
      },
      constraints: false,
    });




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

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



  return files;
};


