import { z } from 'zod';
import { StripeClient } from '../clients/stripe.js';
import { WebhookEndpoint } from '../types/index.js';

const metadataSchema = z.record(z.string()).optional().describe('Set of key-value pairs for storing additional information');

export default [
  {
    name: 'stripe_list_webhook_endpoints',
    description: 'List all webhook endpoints',
    inputSchema: z.object({
      limit: z.number().min(1).max(100).optional().describe('Number of webhook endpoints to return (1-100)'),
      starting_after: z.string().optional().describe('Cursor for pagination'),
      ending_before: z.string().optional().describe('Cursor for pagination')
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.list<WebhookEndpoint>('/webhook_endpoints', args);
    }
  },
  {
    name: 'stripe_get_webhook_endpoint',
    description: 'Retrieve a specific webhook endpoint by ID',
    inputSchema: z.object({
      webhook_endpoint_id: z.string().describe('The ID of the webhook endpoint to retrieve'),
      expand: z.array(z.string()).optional().describe('Fields to expand')
    }),
    handler: async (client: StripeClient, args: any) => {
      const { webhook_endpoint_id, expand } = args;
      const params = expand ? { expand } : undefined;
      return await client.retrieve<WebhookEndpoint>('/webhook_endpoints', webhook_endpoint_id, params);
    }
  },
  {
    name: 'stripe_create_webhook_endpoint',
    description: 'Create a new webhook endpoint',
    inputSchema: z.object({
      url: z.string().url().describe('The URL to send webhook events to'),
      enabled_events: z.array(z.string()).describe('Array of event types to subscribe to (e.g., ["charge.succeeded", "customer.created"]). Use ["*"] for all events.'),
      api_version: z.string().optional().describe('Stripe API version (e.g., "2024-01-18")'),
      connect: z.boolean().optional().describe('Whether to receive events from connected accounts'),
      description: z.string().optional().describe('Arbitrary description'),
      metadata: metadataSchema
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.create<WebhookEndpoint>('/webhook_endpoints', args, {
        idempotencyKey: client.generateIdempotencyKey()
      });
    }
  },
  {
    name: 'stripe_update_webhook_endpoint',
    description: 'Update a webhook endpoint',
    inputSchema: z.object({
      webhook_endpoint_id: z.string().describe('The ID of the webhook endpoint to update'),
      url: z.string().url().optional().describe('New URL'),
      enabled_events: z.array(z.string()).optional().describe('New array of event types'),
      disabled: z.boolean().optional().describe('Disable the webhook endpoint'),
      description: z.string().optional(),
      metadata: metadataSchema
    }),
    handler: async (client: StripeClient, args: any) => {
      const { webhook_endpoint_id, ...data } = args;
      return await client.update<WebhookEndpoint>('/webhook_endpoints', webhook_endpoint_id, data);
    }
  },
  {
    name: 'stripe_delete_webhook_endpoint',
    description: 'Delete a webhook endpoint',
    inputSchema: z.object({
      webhook_endpoint_id: z.string().describe('The ID of the webhook endpoint to delete')
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.remove('/webhook_endpoints', args.webhook_endpoint_id);
    }
  }
];
