/**
 * Webhooks API
 *
 * Endpoints for managing webhook subscriptions (admin only)
 */

import { get, post, patch, del } from './client';
import type { WebhookResponse, SuccessResponse } from '@/types/api';

// TODO: These types should be moved to @/types/api.ts
export interface WebhookListResponse {
  webhooks: WebhookResponse[];
  total: number;
  page: number;
  pageSize: number;
}

export interface CreateWebhookRequest {
  url: string;
  eventTypes: string[];
  secret?: string;
  isActive?: boolean;
  description?: string;
}

export interface UpdateWebhookRequest {
  url?: string;
  eventTypes?: string[];
  secret?: string;
  isActive?: boolean;
  description?: string;
}

export interface WebhookEventListResponse {
  events: Array<{
    id: string;
    webhookId: string;
    eventType: string;
    status: 'success' | 'failure' | 'pending';
    requestBody: any;
    responseStatus?: number;
    responseBody?: any;
    error?: string;
    createdAt: string;
  }>;
  total: number;
  page: number;
  pageSize: number;
}

export interface TestWebhookRequest {
  eventType?: string;
  payload?: any;
}

/**
 * List webhooks with pagination
 * GET /api/v1/webhooks?page=1&limit=20&isActive=true&eventType=booking.created
 * Requires: ADMIN role
 */
export async function listWebhooks(params?: {
  page?: number;
  limit?: number;
  isActive?: boolean;
  eventType?: string;
}): Promise<WebhookListResponse> {
  const queryParams = new URLSearchParams();
  if (params?.page) queryParams.append('page', params.page.toString());
  if (params?.limit) queryParams.append('limit', params.limit.toString());
  if (params?.isActive !== undefined) queryParams.append('isActive', params.isActive.toString());
  if (params?.eventType) queryParams.append('eventType', params.eventType);

  const queryString = queryParams.toString();
  return get<WebhookListResponse>(`/api/v1/webhooks${queryString ? `?${queryString}` : ''}`);
}

/**
 * Get webhook by ID
 * GET /api/v1/webhooks/:id
 * Requires: ADMIN role
 */
export async function getWebhook(id: string): Promise<WebhookResponse> {
  return get<WebhookResponse>(`/api/v1/webhooks/${id}`);
}

/**
 * Create webhook subscription
 * POST /api/v1/webhooks
 * Requires: ADMIN role
 */
export async function createWebhook(data: CreateWebhookRequest): Promise<WebhookResponse> {
  return post<WebhookResponse>('/api/v1/webhooks', data);
}

/**
 * Update webhook
 * PATCH /api/v1/webhooks/:id
 * Requires: ADMIN role
 */
export async function updateWebhook(
  id: string,
  data: UpdateWebhookRequest
): Promise<WebhookResponse> {
  return patch<WebhookResponse>(`/api/v1/webhooks/${id}`, data);
}

/**
 * Delete webhook
 * DELETE /api/v1/webhooks/:id
 * Requires: ADMIN role
 */
export async function deleteWebhook(id: string): Promise<SuccessResponse> {
  return del<SuccessResponse>(`/api/v1/webhooks/${id}`);
}

/**
 * Test webhook (send test event)
 * POST /api/v1/webhooks/:id/test
 * Requires: ADMIN role
 */
export async function testWebhook(id: string, data?: TestWebhookRequest): Promise<SuccessResponse> {
  return post<SuccessResponse>(`/api/v1/webhooks/${id}/test`, data);
}

/**
 * List webhook events (delivery history)
 * GET /api/v1/webhooks/:id/events?page=1&limit=50&status=success
 * Requires: ADMIN role
 */
export async function listWebhookEvents(
  id: string,
  params?: { page?: number; limit?: number; status?: string }
): Promise<WebhookEventListResponse> {
  const queryParams = new URLSearchParams();
  if (params?.page) queryParams.append('page', params.page.toString());
  if (params?.limit) queryParams.append('limit', params.limit.toString());
  if (params?.status) queryParams.append('status', params.status);

  const queryString = queryParams.toString();
  return get<WebhookEventListResponse>(
    `/api/v1/webhooks/${id}/events${queryString ? `?${queryString}` : ''}`
  );
}
