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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"processing",


"ready",


"failed"

        ],

      },

watermark_text: {
        type: DataTypes.TEXT,
      
      

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  download_jobs.associate = (db) => {


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









































//end loop



    db.download_jobs.belongsTo(db.media_items, {
      as: 'media_item',
      foreignKey: {
        name: 'media_itemId',
      },
      constraints: false,
    });

    db.download_jobs.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

    db.download_jobs.belongsTo(db.watermark_profiles, {
      as: 'watermark_profile',
      foreignKey: {
        name: 'watermark_profileId',
      },
      constraints: false,
    });

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



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


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

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



  return download_jobs;
};


