import type { WrikeClient } from '../clients/wrike.js';
import type { WrikeWebhook, CreateWebhookRequest } from '../types/index.js';

export function createWebhookTools(client: WrikeClient) {
  return {
    // List webhooks
    wrike_list_webhooks: {
      name: 'wrike_list_webhooks',
      description: 'List all webhooks',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const response = await client.get<WrikeWebhook>('/webhooks');
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

    // Get webhook by ID
    wrike_get_webhook: {
      name: 'wrike_get_webhook',
      description: 'Get a specific webhook by ID',
      inputSchema: {
        type: 'object',
        properties: {
          webhookId: { type: 'string', description: 'Webhook ID (required)' },
        },
        required: ['webhookId'],
      },
      handler: async (params: { webhookId: string }) => {
        const response = await client.get<WrikeWebhook>(`/webhooks/${params.webhookId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Create webhook
    wrike_create_webhook: {
      name: 'wrike_create_webhook',
      description: 'Create a new webhook',
      inputSchema: {
        type: 'object',
        properties: {
          hookUrl: { type: 'string', description: 'Webhook URL (required)' },
          folderId: { type: 'string', description: 'Folder ID to watch' },
          taskId: { type: 'string', description: 'Task ID to watch' },
        },
        required: ['hookUrl'],
      },
      handler: async (params: { hookUrl: string; folderId?: string; taskId?: string }) => {
        const body: CreateWebhookRequest = {
          hookUrl: params.hookUrl,
        };
        
        if (params.folderId) body.folderId = params.folderId;
        if (params.taskId) body.taskId = params.taskId;
        
        const response = await client.post<WrikeWebhook>('/webhooks', body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update webhook
    wrike_update_webhook: {
      name: 'wrike_update_webhook',
      description: 'Update an existing webhook',
      inputSchema: {
        type: 'object',
        properties: {
          webhookId: { type: 'string', description: 'Webhook ID (required)' },
          status: { type: 'string', enum: ['Active', 'Suspended'], description: 'Webhook status' },
        },
        required: ['webhookId'],
      },
      handler: async (params: { webhookId: string; status?: string }) => {
        const body: Record<string, unknown> = {};
        
        if (params.status) body.status = params.status;
        
        const response = await client.put<WrikeWebhook>(`/webhooks/${params.webhookId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Delete webhook
    wrike_delete_webhook: {
      name: 'wrike_delete_webhook',
      description: 'Delete a webhook',
      inputSchema: {
        type: 'object',
        properties: {
          webhookId: { type: 'string', description: 'Webhook ID (required)' },
        },
        required: ['webhookId'],
      },
      handler: async (params: { webhookId: string }) => {
        const response = await client.delete<WrikeWebhook>(`/webhooks/${params.webhookId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },
  };
}
