/**
 * HubSpot Webhooks Tools
 * Tools for managing webhook subscriptions
 */

import type { HubSpotClient } from '../clients/hubspot.js';

export function getTools(client: HubSpotClient) {
  return [
    {
      name: 'hubspot_list_webhooks',
      description: 'List all webhook subscriptions',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of webhooks to return',
            default: 20,
          },
          after: {
            type: 'string',
            description: 'Pagination cursor',
          },
        },
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/webhooks/v3/subscriptions',
          params: {
            limit: args.limit || 20,
            after: args.after,
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_get_webhook',
      description: 'Get a specific webhook subscription by ID',
      inputSchema: {
        type: 'object',
        properties: {
          webhookId: {
            type: 'string',
            description: 'Webhook subscription ID',
          },
        },
        required: ['webhookId'],
      },
      handler: async (args: any) => {
        const webhook = await client.apiRequest<any>({
          method: 'GET',
          url: `/webhooks/v3/subscriptions/${args.webhookId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(webhook, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_create_webhook',
      description: 'Create a new webhook subscription',
      inputSchema: {
        type: 'object',
        properties: {
          eventType: {
            type: 'string',
            description: 'Event type to subscribe to (e.g., contact.creation, deal.propertyChange)',
          },
          targetUrl: {
            type: 'string',
            description: 'Target URL to receive webhook notifications',
          },
          active: {
            type: 'boolean',
            description: 'Whether the webhook is active',
            default: true,
          },
          propertyName: {
            type: 'string',
            description: 'Specific property name (for propertyChange events)',
          },
        },
        required: ['eventType', 'targetUrl'],
      },
      handler: async (args: any) => {
        const data: any = {
          eventType: args.eventType,
          active: args.active !== false,
        };

        // For propertyChange events, include propertyName
        if (args.propertyName) {
          data.propertyName = args.propertyName;
        }

        const webhook = await client.apiRequest<any>({
          method: 'POST',
          url: '/webhooks/v3/subscriptions',
          data: {
            ...data,
            webhookUrl: args.targetUrl,
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  webhookId: webhook.id,
                  webhook,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_update_webhook',
      description: 'Update an existing webhook subscription',
      inputSchema: {
        type: 'object',
        properties: {
          webhookId: {
            type: 'string',
            description: 'Webhook subscription ID',
          },
          active: {
            type: 'boolean',
            description: 'Whether the webhook is active',
          },
          targetUrl: {
            type: 'string',
            description: 'Updated target URL',
          },
        },
        required: ['webhookId'],
      },
      handler: async (args: any) => {
        const updates: any = {};
        if (args.active !== undefined) updates.active = args.active;
        if (args.targetUrl) updates.webhookUrl = args.targetUrl;

        const webhook = await client.apiRequest<any>({
          method: 'PATCH',
          url: `/webhooks/v3/subscriptions/${args.webhookId}`,
          data: updates,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  webhook,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_delete_webhook',
      description: 'Delete a webhook subscription',
      inputSchema: {
        type: 'object',
        properties: {
          webhookId: {
            type: 'string',
            description: 'Webhook subscription ID to delete',
          },
        },
        required: ['webhookId'],
      },
      handler: async (args: any) => {
        await client.apiRequest({
          method: 'DELETE',
          url: `/webhooks/v3/subscriptions/${args.webhookId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Webhook ${args.webhookId} deleted successfully`,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },
  ];
}
