// Webhooks Tools

import { CalendlyClient } from '../clients/calendly.js';

export function createWebhooksTools(client: CalendlyClient) {
  return {
    calendly_list_webhook_subscriptions: {
      description: 'List webhook subscriptions for an organization',
      parameters: {
        type: 'object',
        properties: {
          organization: {
            type: 'string',
            description: 'Organization URI',
          },
          scope: {
            type: 'string',
            enum: ['user', 'organization'],
            description: 'Webhook scope',
          },
          user: {
            type: 'string',
            description: 'User URI (for user-scoped webhooks)',
          },
          count: {
            type: 'number',
            description: 'Number of results per page',
            default: 20,
          },
          page_token: {
            type: 'string',
            description: 'Pagination token',
          },
        },
        required: ['organization', 'scope'],
      },
      handler: async (args: any) => {
        const result = await client.listWebhookSubscriptions({
          organization: args.organization,
          scope: args.scope,
          user: args.user,
          count: args.count,
          page_token: args.page_token,
        });
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_create_webhook_subscription: {
      description: 'Create a new webhook subscription',
      parameters: {
        type: 'object',
        properties: {
          url: {
            type: 'string',
            description: 'Webhook callback URL',
          },
          events: {
            type: 'array',
            items: {
              type: 'string',
            },
            description: 'Array of event types to subscribe to (e.g., ["invitee.created", "invitee.canceled"])',
          },
          organization: {
            type: 'string',
            description: 'Organization URI',
          },
          scope: {
            type: 'string',
            enum: ['user', 'organization'],
            description: 'Webhook scope',
          },
          user: {
            type: 'string',
            description: 'User URI (for user-scoped webhooks)',
          },
          signing_key: {
            type: 'string',
            description: 'Optional signing key for webhook verification',
          },
        },
        required: ['url', 'events', 'organization', 'scope'],
      },
      handler: async (args: any) => {
        const result = await client.createWebhookSubscription({
          url: args.url,
          events: args.events,
          organization: args.organization,
          scope: args.scope,
          user: args.user,
          signing_key: args.signing_key,
        });
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_get_webhook_subscription: {
      description: 'Get details of a specific webhook subscription',
      parameters: {
        type: 'object',
        properties: {
          uuid: {
            type: 'string',
            description: 'Webhook subscription UUID',
          },
        },
        required: ['uuid'],
      },
      handler: async (args: any) => {
        const result = await client.getWebhookSubscription(args.uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_delete_webhook_subscription: {
      description: 'Delete a webhook subscription',
      parameters: {
        type: 'object',
        properties: {
          uuid: {
            type: 'string',
            description: 'Webhook subscription UUID to delete',
          },
        },
        required: ['uuid'],
      },
      handler: async (args: any) => {
        await client.deleteWebhookSubscription(args.uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({ success: true, message: 'Webhook subscription deleted' }),
            },
          ],
        };
      },
    },
  };
}
