import {
  httpClient,
  HttpMethod,
} from '@activepieces/pieces-common';
import {
  createTrigger,
  Property,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import { drupalAuth } from '../auth';

const webhookStoreKey = (id: string) => `_drupal_webhook_trigger_${id}`;

export const drupalWebhook = createTrigger({
  auth: drupalAuth,
  name: 'drupalWebhook',
  displayName: 'Webhook',
  description: 'A webhook that the Drupal site can call to trigger a flow.',
  aiMetadata: {
    description: 'Fires in real time whenever the Drupal site calls the registered webhook URL (e.g. from an ECA action), emitting the request body sent by Drupal. Use for push-based events instead of polling; the webhook is registered on enable under the unique name supplied, which must match the name referenced on the Drupal side.',
  },
  props: {
    id: Property.ShortText({
      displayName: 'Name',
      description: 'This name identifies the webhook. It must be unique. It will be used to identify the webhook in the Drupal site, e.g. if you use ECA to call this webhook, you will find this name in the list of available webhooks.',
      required: true,
    }),
  },
  sampleData: {},
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const { website_url, username, password } = context.auth.props;
    const body: any = {
      id: context.propsValue.id,
      webHookUrl: context.webhookUrl,
    };
    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: website_url + `/orchestration/webhook/register`,
      body: body,
      headers: {
        'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
        'Accept': 'application/vnd.api+json',
      },
    });
    console.debug('Webhook register response', response);
    await context.store.put(webhookStoreKey(context.propsValue.id), response.body);
  },
  async onDisable(context) {
    const { website_url, username, password } = context.auth.props;
    const webhook = await context.store.get(webhookStoreKey(context.propsValue.id));
    if (webhook) {
      const response = await httpClient.sendRequest({
        method: HttpMethod.POST,
        url: website_url + `/orchestration/webhook/unregister`,
        body: webhook,
        headers: {
          'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
          'Accept': 'application/vnd.api+json',
        },
      });
      console.debug('Webhook unregister response', response);
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});
