/* eslint-disable @typescript-eslint/brace-style */
import { getUserLocale } from '../../../integrations/blocklet/notification';
import { translate } from '../../../locales';
import { Customer, Invoice, Subscription } from '../../../store/models';
import { getCustomerInvoicePageUrl } from '../../invoice';
import { getMainProductName } from '../../product';
import { getCustomerSubscriptionPageUrl } from '../../subscription';
import { getSubscriptionNotificationCustomActions } from '../../util';
import type { BaseEmailTemplate, BaseEmailTemplateType } from './base';

export interface SubscriptionSlippageExceededEmailTemplateOptions {
  subscriptionId: string;
  invoiceId: string;
  paymentIntentId: string;
  currentRate: string;
  minAcceptableRate: string;
}

interface SubscriptionSlippageExceededEmailTemplateContext {
  locale: string;
  productName: string;
  userDid: string;
  currentRate: string;
  minAcceptableRate: string;
  viewSubscriptionLink: string;
  viewInvoiceLink: string;
  customActions: any[];
}

export class SubscriptionSlippageExceededEmailTemplate
  implements BaseEmailTemplate<SubscriptionSlippageExceededEmailTemplateContext>
{
  options: SubscriptionSlippageExceededEmailTemplateOptions;

  constructor(options: SubscriptionSlippageExceededEmailTemplateOptions) {
    this.options = options;
  }

  async getContext(): Promise<SubscriptionSlippageExceededEmailTemplateContext> {
    const subscription = await Subscription.findByPk(this.options.subscriptionId);
    if (!subscription) {
      throw new Error(`Subscription not found: ${this.options.subscriptionId}`);
    }

    const customer = await Customer.findByPk(subscription.customer_id);
    if (!customer) {
      throw new Error(`Customer not found: ${subscription.customer_id}`);
    }

    const invoice = await Invoice.findByPk(this.options.invoiceId);
    if (!invoice) {
      throw new Error(`Invoice not found: ${this.options.invoiceId}`);
    }

    const userDid = customer.did;
    const locale = await getUserLocale(userDid);
    const productName = await getMainProductName(subscription.id);

    const viewSubscriptionLink = getCustomerSubscriptionPageUrl({
      subscriptionId: subscription.id,
      locale,
      userDid,
    });

    const viewInvoiceLink = getCustomerInvoicePageUrl({
      invoiceId: invoice.id,
      userDid,
      locale,
      action: 'pay',
    });

    const customActions = getSubscriptionNotificationCustomActions(
      subscription,
      'subscription.slippage_exceeded',
      locale
    );

    return {
      locale,
      productName,
      userDid,
      currentRate: this.options.currentRate,
      minAcceptableRate: this.options.minAcceptableRate,
      viewSubscriptionLink,
      viewInvoiceLink,
      customActions,
    };
  }

  async getTemplate(): Promise<BaseEmailTemplateType> {
    const {
      locale,
      productName,
      userDid,
      currentRate,
      minAcceptableRate,
      viewSubscriptionLink,
      viewInvoiceLink,
      customActions,
    } = await this.getContext();

    const template: BaseEmailTemplateType = {
      title: translate('notification.subscriptionSlippageExceeded.title', locale, {
        productName,
      }),
      body: translate('notification.subscriptionSlippageExceeded.body', locale, {
        productName,
        currentRate,
        minAcceptableRate,
      }),
      // @ts-ignore
      attachments: [
        {
          type: 'section',
          fields: [
            {
              type: 'text',
              data: {
                type: 'plain',
                color: '#9397A1',
                text: translate('notification.common.account', locale),
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                text: userDid,
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                color: '#9397A1',
                text: translate('notification.common.product', locale),
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                text: productName,
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                color: '#9397A1',
                text: translate('notification.subscriptionSlippageExceeded.currentRate', locale),
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                color: '#FF0000',
                text: currentRate,
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                color: '#9397A1',
                text: translate('notification.subscriptionSlippageExceeded.minAcceptableRate', locale),
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                text: minAcceptableRate,
              },
            },
          ],
        },
      ],
      // @ts-ignore
      actions: [
        {
          name: translate('notification.subscriptionSlippageExceeded.payNow', locale),
          title: translate('notification.subscriptionSlippageExceeded.payNow', locale),
          link: viewInvoiceLink,
        },
        {
          name: translate('notification.common.viewSubscription', locale),
          title: translate('notification.common.viewSubscription', locale),
          link: viewSubscriptionLink,
        },
        {
          name: translate('notification.subscriptionSlippageExceeded.adjustSlippage', locale),
          title: translate('notification.subscriptionSlippageExceeded.adjustSlippage', locale),
          link: viewSubscriptionLink,
        },
        ...customActions,
      ].filter(Boolean),
    };

    return template;
  }
}
