import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { chargekeepAuth } from '../auth';
import { chargekeepCommon } from '../common/common';

export const newPayment = createTrigger({
  auth: chargekeepAuth,
  name: 'new_payment',
  displayName: 'New Payment',
  description: 'Triggers when a new payment is created',
  aiMetadata: {
    description: 'Fires when a new payment is recorded in the ChargeKeep/Sperse CRM, emitting the payment transaction along with its related contact and invoice.',
  },
  props: {},
  type: TriggerStrategy.WEBHOOK,
  sampleData: {
    contact: {
      email: 's@gmail.com',
      fullName: 'Test User',
      id: 636,
      phone: '98877676565',
    },
    invoice: {
      currencyId: 'USD',
      date: '2024-06-10T00:00:00Z',
      description: 'just a description',
      discountTotal: 0,
      grandTotal: 100,
      id: 457,
      number: 'INV - 20240610 - 746C',
      shippingTotal: 0,
      taxTotal: 0,
      lines: [
        {
          description: 'Me Spacial',
          productId: 810,
          quantity: 1,
          rate: 100,
          total: 100,
          unitId: 'Month',
        },
      ],
    },
    transaction: {
      amount: 100,
      currencyId: 'USD',
      date: '2024-06-10T09:36:01.832Z',
      gatewayName: null,
      gatewayTransactionId: null,
      id: 403,
      isSuccessful: true,
      type: 'Sale',
    },
    eventTime: '2024-06-10T09:36:08',
    eventType: 'Payment.Created',
  },
  async onEnable(context) {
    const webhookId = await chargekeepCommon.subscribeWebhook(
      'Payment.Created',
      context.auth.props.base_url,
      context.auth.props.api_key,
      context.webhookUrl
    );

    await context.store?.put<WebhookInformation>('_new_payment_trigger', {
      webhookId: webhookId,
    });
  },

  async onDisable(context) {
    const response = await context.store?.get<WebhookInformation>(
      '_new_payment_trigger'
    );

    if (response !== null && response !== undefined) {
      await chargekeepCommon.unsubscribeWebhook(
        context.auth.props.base_url,
        context.auth.props.api_key,
        response.webhookId
      );
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});

interface WebhookInformation {
  webhookId: number;
}
