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,
      
      

      },

file_kind: {
        type: DataTypes.ENUM,
      
      

        values: [

"document",


"image",


"audio",


"video",


"archive",


"other"

        ],

      },

size_mb: {
        type: DataTypes.DECIMAL,
      
      

      },

mime_type: {
        type: DataTypes.TEXT,
      
      

      },

storage_uri: {
        type: DataTypes.TEXT,
      
      

      },

checksum_sha256: {
        type: DataTypes.TEXT,
      
      

      },

scan_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"clean",


"infected",


"failed"

        ],

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

      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.room_files, {
      as: 'room_files_file',
      foreignKey: {
          name: 'fileId',
      },
      constraints: false,
    });































//end loop



    db.files.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

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




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

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



  return files;
};


