import type { BigCommerceClient } from '../clients/bigcommerce.js';
import type { Webhook } from '../types/index.js';

export function registerWebhookTools(client: BigCommerceClient) {
  return {
    bigcommerce_list_webhooks: {
      description: 'List all webhooks',
      parameters: {
        type: 'object' as const,
        properties: {
          scope: { type: 'string', description: 'Filter by scope (e.g., store/order/*, store/product/*)' },
          is_active: { type: 'boolean', description: 'Filter by active status' },
        },
      },
      handler: async (params: { scope?: string; is_active?: boolean }) => {
        const queryParams: Record<string, string | number> = {};
        if (params.scope) queryParams.scope = params.scope;
        if (params.is_active !== undefined) queryParams.is_active = params.is_active ? 'true' : 'false';

        const result = await client.get<{ data: Webhook[] }>('/hooks');
        return result.data;
      },
    },

    bigcommerce_get_webhook: {
      description: 'Get a specific webhook by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          webhook_id: { type: 'number', description: 'Webhook ID', required: true },
        },
        required: ['webhook_id'],
      },
      handler: async (params: { webhook_id: number }) => {
        const result = await client.get<{ data: Webhook }>(`/hooks/${params.webhook_id}`);
        return result.data;
      },
    },

    bigcommerce_create_webhook: {
      description: 'Create a new webhook subscription',
      parameters: {
        type: 'object' as const,
        properties: {
          scope: {
            type: 'string',
            description: 'Event scope (store/order/created, store/order/updated, store/product/created, store/product/updated, store/product/deleted, store/customer/created, store/cart/created, etc.)',
            required: true,
          },
          destination: { type: 'string', description: 'Webhook destination URL (HTTPS required)', required: true },
          is_active: { type: 'boolean', description: 'Active status (default: true)' },
          headers: {
            type: 'object',
            description: 'Custom headers to include in webhook requests (e.g., {"Authorization": "Bearer token"})',
          },
        },
        required: ['scope', 'destination'],
      },
      handler: async (params: Partial<Webhook>) => {
        const result = await client.post<{ data: Webhook }>('/hooks', params);
        return result.data;
      },
    },

    bigcommerce_update_webhook: {
      description: 'Update a webhook',
      parameters: {
        type: 'object' as const,
        properties: {
          webhook_id: { type: 'number', description: 'Webhook ID', required: true },
          scope: { type: 'string', description: 'Event scope' },
          destination: { type: 'string', description: 'Webhook destination URL' },
          is_active: { type: 'boolean', description: 'Active status' },
          headers: { type: 'object', description: 'Custom headers' },
        },
        required: ['webhook_id'],
      },
      handler: async (params: { webhook_id: number; [key: string]: unknown }) => {
        const { webhook_id, ...updateData } = params;
        const result = await client.put<{ data: Webhook }>(`/hooks/${webhook_id}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_webhook: {
      description: 'Delete a webhook subscription',
      parameters: {
        type: 'object' as const,
        properties: {
          webhook_id: { type: 'number', description: 'Webhook ID', required: true },
        },
        required: ['webhook_id'],
      },
      handler: async (params: { webhook_id: number }) => {
        await client.delete(`/hooks/${params.webhook_id}`);
        return { success: true, message: `Webhook ${params.webhook_id} deleted` };
      },
    },

    bigcommerce_list_webhook_scopes: {
      description: 'List all available webhook scopes/events',
      parameters: {
        type: 'object' as const,
        properties: {},
      },
      handler: async () => {
        // Return common webhook scopes
        return {
          scopes: [
            'store/order/created',
            'store/order/updated',
            'store/order/archived',
            'store/order/statusUpdated',
            'store/product/created',
            'store/product/updated',
            'store/product/deleted',
            'store/product/inventory/updated',
            'store/product/inventory/order/updated',
            'store/customer/created',
            'store/customer/updated',
            'store/customer/deleted',
            'store/cart/created',
            'store/cart/updated',
            'store/cart/deleted',
            'store/cart/couponApplied',
            'store/cart/abandoned',
            'store/cart/converted',
            'store/shipment/created',
            'store/shipment/updated',
            'store/shipment/deleted',
            'store/subscriber/created',
            'store/subscriber/updated',
            'store/subscriber/deleted',
            'store/category/created',
            'store/category/updated',
            'store/category/deleted',
            'store/sku/created',
            'store/sku/updated',
            'store/sku/deleted',
          ],
        };
      },
    },
  };
}
