/* eslint-disable prettier/prettier */
import { translate } from '../../../locales';
import { Customer, Subscription } from '../../../store/models';
import type { BaseEmailTemplate, BaseEmailTemplateType } from './base';
import { getUserLocale } from '../../../integrations/blocklet/notification';
import { getCustomerSubscriptionPageUrl, isSubscriptionOverdraftProtectionEnabled } from '../../subscription';
import { getMainProductName } from '../../product';

export interface OverdraftProtectionExhaustedEmailTemplateOptions {
  subscriptionId: string;
}

interface OverdraftProtectionExhaustedEmailTemplateContext {
  locale: string;
  userDid: string;
  viewSubscriptionLink: string;
  productName: string;
  subscriptionId: string;
}

export class OverdraftProtectionExhaustedEmailTemplate
  implements BaseEmailTemplate<OverdraftProtectionExhaustedEmailTemplateContext> {
  options: OverdraftProtectionExhaustedEmailTemplateOptions;

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

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

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

      if (!subscription.overdraft_protection?.enabled) {
        throw new Error(`Overdraft protection not enabled for subscription: ${subscriptionId}`);
      }

     const { enabled } = await isSubscriptionOverdraftProtectionEnabled(subscription);
     if (enabled) {
      throw new Error(`Overdraft protection enabled for subscription: ${subscriptionId}`);
     }
     
    const productName = await getMainProductName(subscription.id);
    const userDid = customer.did;
    const locale = await getUserLocale(userDid);
    const viewSubscriptionLink = getCustomerSubscriptionPageUrl({
      subscriptionId: subscription.id,
      locale,
      userDid,
    });
    return {
      userDid,
      locale,
      viewSubscriptionLink,
      productName,
      subscriptionId,
    };
  }

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

    const template: BaseEmailTemplateType = {
      title: translate('notification.overdraftProtectionExhausted.title', locale),
      body: translate('notification.overdraftProtectionExhausted.body', locale, {
        productName
      }),
      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.common.subscriptionId', locale),
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                text: subscriptionId,
              },
            },
          ].filter(Boolean),
        },
      ],
      actions: [
        {
          name: translate('notification.common.viewSubscription', locale),
          title: translate('notification.common.viewSubscription', locale),
          link: viewSubscriptionLink,
        },
      ].filter(Boolean),
    };

    return template;
  }
}
