/**
 * Basecamp Webhooks Tools
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Webhook, CreateWebhookRequest, UpdateWebhookRequest } from '../types/index.js';

export function registerWebhooksTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_webhooks_list',
      description: 'List all webhooks for the account',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const { data } = await client.get<Webhook[]>('/webhooks.json');
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_webhook_create',
      description: 'Create a new webhook',
      inputSchema: {
        type: 'object',
        properties: {
          payload_url: {
            type: 'string',
            description: 'The URL to send webhook payloads to',
          },
          types: {
            type: 'array',
            items: { type: 'string' },
            description: 'Array of event types to subscribe to (e.g., ["Todo", "Comment"])',
          },
        },
        required: ['payload_url'],
      },
      handler: async (args: CreateWebhookRequest) => {
        const data = await client.post<Webhook>('/webhooks.json', args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_webhook_update',
      description: 'Update a webhook',
      inputSchema: {
        type: 'object',
        properties: {
          webhook_id: {
            type: 'number',
            description: 'The ID of the webhook',
          },
          payload_url: {
            type: 'string',
            description: 'Updated payload URL',
          },
          types: {
            type: 'array',
            items: { type: 'string' },
            description: 'Updated event types',
          },
        },
        required: ['webhook_id'],
      },
      handler: async (args: { webhook_id: number } & UpdateWebhookRequest) => {
        const { webhook_id, ...updates } = args;
        const data = await client.put<Webhook>(`/webhooks/${webhook_id}.json`, updates);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_webhook_delete',
      description: 'Delete a webhook',
      inputSchema: {
        type: 'object',
        properties: {
          webhook_id: {
            type: 'number',
            description: 'The ID of the webhook',
          },
        },
        required: ['webhook_id'],
      },
      handler: async (args: { webhook_id: number }) => {
        await client.delete(`/webhooks/${args.webhook_id}.json`);
        return {
          content: [
            {
              type: 'text',
              text: `Webhook ${args.webhook_id} deleted successfully`,
            },
          ],
        };
      },
    },
  ];
}
