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

'use strict';

const mongoose = require('mongoose');
const Announcement = mongoose.model('Announcement');

const { SiteService } = require('../../lib/site-lib');

class AnnouncementService extends SiteService {

  constructor (dtp) {
    super(dtp, module.exports);
  }

  async create (announcementDefinition) {
    const NOW = new Date();
    const { coreNode: coreNodeService } = this.dtp.services;

    const announcement = new Announcement();
    announcement.created = NOW;
    announcement.title = {
      icon: {
        class: announcementDefinition.titleIconClass,
        color: announcementDefinition.titleIconColor,
      },
      content: announcementDefinition.titleContent,
    };
    announcement.content = announcementDefinition.content.trim();

    await announcement.save();

    /*
     * Broadcast the Announcement to your DTP Constellation.
     */
    const announcementHref = coreNodeService.getLocalUrl(`/announcement/${announcement._id}`);
    await coreNodeService.sendKaleidoscopeEvent({
      action: 'announcement',
      label: announcement.title.content,
      content: announcement.content,
      href: announcementHref,
    });

    return announcement.toObject();
  }

  async update (announcement, announcementDefinition) {
    await Announcement.updateOne(
      { _id: announcement._id },
      {
        $set: {
          title: {
            icon: {
              class: announcementDefinition.titleIconClass,
              color: announcementDefinition.titleIconColor,
            },
            content: announcementDefinition.titleContent,
          },
          content: announcementDefinition.content.trim(),
        },
      },
    );
  }

  async getLatest (user) {
    const { user: userService } = this.dtp.services;
    let announcements = [ ];

    if (user) {
      const search = { };
      if (user.lastAnnouncement) {
        search.created = { $gt: user.lastAnnouncement };
      }
      announcements = await Announcement
        .find(search)
        .sort({ created: -1 })
        .limit(1)
        .lean();
      if (announcements && (announcements.length > 0)) {
        await userService.setLastAnnouncement(user, announcements[0]);
      }
    } else {
      announcements = await Announcement
        .find()
        .sort({ created: -1 })
        .limit(1)
        .lean();
    }

    return announcements;
  }

  async getById (announcementId) {
    const announcement = await Announcement.findById(announcementId);
    return announcement;
  }

  async getAnnouncements (pagination) {
    const announcements = await Announcement
      .find()
      .sort({ created: -1 })
      .skip(pagination.skip)
      .limit(pagination.cpp)
      .lean();
    return announcements;
  }

  async remove (announcement) {
    const { comment: commentService } = this.dtp.services;
    await commentService.deleteForResource(announcement);

    await Announcement.deleteOne({ _id: announcement._id });
  }
}

module.exports = {
  slug: 'announcement',
  name: 'announcement',
  create: (dtp) => { return new AnnouncementService(dtp); },
};