import { getUrl } from '@blocklet/sdk/lib/component';
import { withQuery } from 'ufo';
import { getConnectQueryParam, getOwnerDid } from '../../util';
import { translate } from '../../../locales';
import { WebhookEndpoint } from '../../../store/models';
import type { BaseEmailTemplate, BaseEmailTemplateType } from './base';
import { getUserLocale } from '../../../integrations/blocklet/notification';

export interface WebhookEndpointFailedEmailTemplateOptions {
  webhookEndpointId: string;
  failedCount: number;
}

interface WebhookEndpointFailedEmailTemplateContext {
  locale: string;
  userDid: string;
  webhookDescription: string;
  failedCount: number;
  webhookEndpointId: string;
  viewWebhookLink: string;
}

// eslint-disable-next-line prettier/prettier
export class WebhookEndpointFailedEmailTemplate implements BaseEmailTemplate<WebhookEndpointFailedEmailTemplateContext> {
  options: WebhookEndpointFailedEmailTemplateOptions;

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

  async getContext(): Promise<WebhookEndpointFailedEmailTemplateContext> {
    const { webhookEndpointId, failedCount } = this.options;

    const webhookEndpoint = await WebhookEndpoint.findByPk(webhookEndpointId);
    if (!webhookEndpoint) {
      throw new Error(`WebhookEndpoint not found: ${webhookEndpointId}`);
    }

    const userDid = await getOwnerDid();
    if (!userDid) {
      throw new Error('get owner did failed');
    }

    const locale = await getUserLocale(userDid);

    const viewWebhookLink = getUrl(
      withQuery(`admin/developers/${webhookEndpointId}`, {
        locale,
        ...getConnectQueryParam({ userDid }),
      })
    );

    return {
      userDid,
      locale,
      webhookDescription: webhookEndpoint.description || '',
      failedCount,
      webhookEndpointId,
      viewWebhookLink,
    };
  }

  async getTemplate(): Promise<BaseEmailTemplateType> {
    const { locale, webhookDescription, failedCount, webhookEndpointId, viewWebhookLink } = await this.getContext();

    const template: BaseEmailTemplateType = {
      title: translate('notification.webhookEndpointFailed.title', locale, {
        failedCount,
      }),
      body: translate('notification.webhookEndpointFailed.body', locale, {
        failedCount,
      }),
      attachments: [
        {
          type: 'section',
          fields: [
            {
              type: 'text',
              data: {
                type: 'plain',
                color: '#9397A1',
                text: translate('notification.common.webhookEndpointId', locale),
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                text: webhookEndpointId,
              },
            },
            webhookDescription
              ? {
                  type: 'text',
                  data: {
                    type: 'plain',
                    color: '#9397A1',
                    text: translate('notification.common.description', locale),
                  },
                }
              : null,
            webhookDescription
              ? {
                  type: 'text',
                  data: {
                    type: 'plain',
                    text: webhookDescription,
                  },
                }
              : null,
            {
              type: 'text',
              data: {
                type: 'plain',
                color: '#9397A1',
                text: translate('notification.common.failedCount', locale),
              },
            },
            {
              type: 'text',
              data: {
                type: 'plain',
                text: failedCount.toString(),
              },
            },
          ].filter(Boolean),
        },
      ],
      actions: [
        {
          name: translate('notification.common.viewWebhook', locale),
          title: translate('notification.common.viewWebhook', locale),
          link: viewWebhookLink,
        },
      ].filter(Boolean),
    };

    return template;
  }
}
