// newsroom.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 { read: feedReader } = require('feed-reader');

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

module.rootPath = path.resolve(__dirname, '..', '..');
module.pkg = require(path.resolve(__dirname, '..', '..', 'package.json'));

module.config = {
  environment: process.env.NODE_ENV,
  root: module.rootPath,
  component: { name: 'newsroom', slug: 'newsroom' },
};

module.config.site = require(path.join(module.rootPath, 'config', 'site'));
module.config.http = require(path.join(module.rootPath, 'config', 'http'));

class NewsroomWorker extends SiteWorker {

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

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

    await this.loadProcessor(path.join(__dirname, 'newsroom', 'cron', 'update-feeds.js'));
    await this.loadProcessor(path.join(__dirname, 'newsroom', 'job', 'update-feed.js'));

    await this.startProcessors();
  }

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

  async updateFeed (feed) {
    const Feed = mongoose.model('Feed');
    const NOW = new Date();
    const { feed: feedService } = this.dtp.services;
    try {
      this.log.info('loading latest feed data', { feedId: feed._id, title: feed.title });
      const response = await feedReader(feed.url);
      await SiteAsync.each(response.entries, async (entry) => {
        await Feed.updateOne({ _id: feed._id }, { $set: { published: feed.published || NOW }});
        await feedService.createEntry(feed, entry);
      }, 4);
      this.log.info('feed updated', { entries: response.entries.length });
    } catch (error) {
      this.log.error('failed to update feed', { feedId: feed._id, title: feed.title, error });
    }
  }
}

(async ( ) => {
  try {
    module.log = new SiteLog(module, module.config.component);

    module.worker = new NewsroomWorker(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);
  }

})();