// media.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '..', '..', '.env') });

const mongoose = require('mongoose');

const { SitePlatform, SiteLog, SiteWorker } = require(path.join(__dirname, '..', '..', 'lib', 'site-lib'));

module.pkg = require(path.resolve(__dirname, '..', '..', 'package.json'));
module.config = {
  environment: process.env.NODE_ENV,
  root: path.resolve(__dirname, '..', '..'),
  component: { name: 'mediaWorker', slug: 'media-worker' },
};

/**
 * Provides background media processing for the DTP ecosystem.
 *
 * Background media processing is simply a way of life for scalable Web
 * architectures. You don't want to force your site member to sit there and
 * watch a process run. You want to accept their file, toss it to storage, and
 * create a job to have whatever work needs done performed.
 *
 * This obviously induces a variable amount of time from when the site member
 * uploads the file until it's ready for online distribution. The system
 * therefore facilitates ways to query the status of the job and to receive a
 * notification when the work is complete.
 *
 * This worker serves as a starting point or demonstration of how to do
 * background media processing at scale and in production. This is the exact
 * code we use to run the Digital Telepresence Platform every day.
 */
class MediaWorker extends SiteWorker {

  constructor (dtp) {
    super(dtp, dtp.config.component);
  }

  async start ( ) {
    await super.start();

    if (process.argv[2]) {
      const stickerId = mongoose.Types.ObjectId(process.argv[2]);
      this.log.info('creating sticker processing job', { stickerId });

      const queue = this.getJobQueue('media', this.dtp.config.jobQueues.media);
      await queue.add('sticker-ingest', { stickerId });
    }

    await this.loadProcessor(path.join(__dirname, 'media', 'job', 'sticker-ingest.js'));
    await this.loadProcessor(path.join(__dirname, 'media', 'job', 'sticker-delete.js'));

    await this.loadProcessor(path.join(__dirname, 'media', 'job', 'attachment-ingest.js'));
    await this.loadProcessor(path.join(__dirname, 'media', 'job', 'attachment-delete.js'));

    await this.loadProcessor(path.join(__dirname, 'media', 'job', 'webpage-screenshot.js'));

    await this.startProcessors();
  }

  async stop ( ) {
    await super.stop();
  }
}

(async ( ) => {
  try {
    module.log = new SiteLog(module, module.config.component);
    await SitePlatform.startPlatform(module, module.config.component);
    
    module.worker = new MediaWorker(module);
    await module.worker.start();
    
    module.log.info(`${module.pkg.name} v${module.pkg.version} ${module.config.component.name} started`);
  } catch (error) {
    module.log.error('failed to start worker', {
      component: module.config.component,
      error,
    });
    process.exit(-1);
  }
})();