import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { savvyCalApiCall, verifyWebhookSignature } from '../common';
import { savvyCalAuth, getToken } from '../auth';

const SAMPLE_DATA = {
  event_type: 'workflow.action.triggered',
  id: 'wf_abc123',
  action: 'send_email',
};

export const workflowActionTriggeredTrigger = createTrigger({
  auth: savvyCalAuth,
  name: 'workflow_action_triggered',
  displayName: 'Workflow Action Triggered',
  description: 'Triggers when a workflow action is triggered in SavvyCal.',
  aiMetadata: {
    description: 'Fires via webhook when a SavvyCal automation workflow runs one of its configured actions. Each firing represents a single workflow action execution with its associated payload.',
  },
  props: {},
  sampleData: SAMPLE_DATA,
  type: TriggerStrategy.WEBHOOK,

  async onEnable(context) {
    const response = await savvyCalApiCall<{ id: string; secret: string }>({
      token: getToken(context.auth),
      method: HttpMethod.POST,
      path: '/webhooks',
      body: { url: context.webhookUrl },
    });
    await context.store.put('webhookId', response.body.id);
    await context.store.put('webhookSecret', response.body.secret);
  },

  async onDisable(context) {
    const webhookId = await context.store.get<string>('webhookId');
    if (webhookId) {
      await savvyCalApiCall({
        token: getToken(context.auth),
        method: HttpMethod.DELETE,
        path: `/webhooks/${webhookId}`,
      });
    }
  },

  async run(context) {
    const secret = await context.store.get<string>('webhookSecret');
    const signature = context.payload.headers['x-savvycal-signature'] as string | undefined;
    if (secret && (!signature || !verifyWebhookSignature(secret, signature, context.payload.rawBody))) {
      return [];
    }

    const body = context.payload.body as { type: string; payload: Record<string, unknown> };
    if (!body?.payload || body.type !== 'workflow.action.triggered') return [];

    return [{ event_type: body.type, ...body.payload }];
  },

  async test(_context) {
    return [SAMPLE_DATA];
  },
});
