import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { Bid } from '@/modules/bids/entities/bid.entity';
import { Notification } from '../entities/notification.entity';
import { BotTelegramApi } from '@/modules/bids/apis/bot-telegram.api';
import { MailsService } from '@/modules/mails/services/mails.service';
import { ConfigsService } from '@/modules/bids/services/configs.service';
import * as moment from 'moment';
import { Event } from '@/modules/bids/utils/events';

@Injectable()
export class AdminNotificationListener {
  constructor(
    private readonly botTelegramApi: BotTelegramApi,
    private readonly mailsService: MailsService,
    private readonly configsSerice: ConfigsService,
  ) {}

  @OnEvent(Event.BID_STATUS)
  async handleBidStatus({
    bid,
    notification,
  }: {
    bid: Bid;
    notification: Notification;
  }) {
    if (JSON.parse(notification.send_to).length <= 0) return;

    // this.botTelegramApi.sendMessage(notification.message);

    const mails =
      (await this.configsSerice.getConfig('MAIL_SCRAP_REPORT')).value || '';

    this.mailsService.sendHtmlMailJob({
      to: mails,
      html: this.mailsService.getAuctionStatusEmailContent(bid),
      subject:
        `[${bid.status.toUpperCase()}] Report Auto Auctions System ` +
        moment(new Date()).format('YYYY-MM-DD HH:mm'),
    });
  }

  @OnEvent(Event.BID_SUBMITED)
  async handleBidSubmited(bid: Bid) {
    const mails =
      (await this.configsSerice.getConfig('MAIL_SCRAP_REPORT')).value || '';

    this.mailsService.sendHtmlMailJob({
      to: mails,
      html: this.mailsService.getBidSubmittedEmailContent(bid),
      subject:
        'Report Auto Auctions System ' +
        moment(new Date()).format('YYYY-MM-DD HH:mm'),
    });
  }

  @OnEvent(Event.BID_DEMO)
  async handleBidDemo({ bid, type }: { bid: Bid; type: 'api' | 'action' }) {
    const mails =
      (await this.configsSerice.getConfig('MAIL_SCRAP_REPORT')).value || '';

    const html = await this.mailsService.getBidRecordEmailContent(bid, type);

    this.mailsService.sendHtmlMailJob({
      to: mails,
      html: html,
      subject:
        'Demo Auto Auctions System ' +
        moment(new Date()).format('YYYY-MM-DD HH:mm'),
    });
  }

  @OnEvent(Event.SEND_WARNING)
  async handleSendWarning({
    title,
    messages,
  }: {
    title: string;
    messages: string[];
  }) {
    const mails =
      (await this.configsSerice.getConfig('MAIL_SCRAP_REPORT')).value || '';

    const html = this.mailsService.generateWarningEmailTemplate(
      title,
      messages,
    );

    this.mailsService.sendHtmlMailJob({
      to: mails,
      html: html,
      subject:
        '[WARNING] Auto Auctions System ' +
        moment(new Date()).format('YYYY-MM-DD HH:mm'),
    });
  }

  @OnEvent(Event.SEND_SUCCESS)
  async handleSendSuccess({
    title,
    messages,
  }: {
    title: string;
    messages: string[];
  }) {
    const mails =
      (await this.configsSerice.getConfig('MAIL_SCRAP_REPORT')).value || '';

    const html = this.mailsService.generateSuccessEmailTemplate(
      title,
      messages,
    );

    this.mailsService.sendHtmlMailJob({
      to: mails,
      html: html,
      subject:
        '[SUCCESS] Auto Auctions System ' +
        moment(new Date()).format('YYYY-MM-DD HH:mm'),
    });
  }

  @OnEvent(Event.SEND_ERROR)
  async handleSendError({
    title,
    messages,
  }: {
    title: string;
    messages: string[];
  }) {
    const mails =
      (await this.configsSerice.getConfig('MAIL_SCRAP_REPORT')).value || '';

    const html = this.mailsService.generateErrorEmailTemplate(title, messages);

    this.mailsService.sendHtmlMailJob({
      to: mails,
      html: html,
      subject:
        '[ERROR] Auto Auctions System ' +
        moment(new Date()).format('YYYY-MM-DD HH:mm'),
    });
  }

  @OnEvent(Event.SEND_INFO)
  async handleSendInfo({
    title,
    messages,
  }: {
    title: string;
    messages: string[];
  }) {
    const mails =
      (await this.configsSerice.getConfig('MAIL_SCRAP_REPORT')).value || '';

    const html = this.mailsService.generateInfoEmailTemplate(title, messages);

    this.mailsService.sendHtmlMailJob({
      to: mails,
      html: html,
      subject:
        '[INFO] Auto Auctions System ' +
        moment(new Date()).format('YYYY-MM-DD HH:mm'),
    });
  }
}
