/**
 * Webhook API - Feature API Layer
 * Action 6.1.1.10: Update feature API files to use services
 * 
 * This file provides the implementation layer for webhook API operations.
 * Currently, there is no unified service layer for webhooks.
 * 
 * TODO: Consider creating @/services/api/webhooks (webhooksApi) in the future
 * to align with the service layer pattern used for tracks, auth, users, and playlists.
 */

import { apiClient } from '@/services/api/client';
import { Webhook } from '@/types/webhook';

/**
 * Webhook API
 * Implémente les endpoints webhooks selon le backend
 * Endpoints:
 * - POST /webhooks (protected) - Enregistrer un webhook
 * - GET /webhooks (protected) - Lister les webhooks de l'utilisateur
 * - DELETE /webhooks/:id (protected) - Supprimer un webhook
 * - GET /webhooks/stats (protected) - Statistiques des webhooks
 * - POST /webhooks/:id/test (protected) - Tester un webhook
 * - POST /webhooks/:id/regenerate-key (protected) - Régénérer la clé API
 */

export interface RegisterWebhookRequest {
  url: string;
  events: string[]; // Array of event types (e.g., ['track.uploaded', 'user.created'])
}

export interface WebhookStats {
  queue_size: number;
  workers: number;
  max_retries: number;
}

export interface WebhookStatsResponse {
  user_id: string;
  stats: WebhookStats;
}

export interface RegenerateAPIKeyResponse {
  api_key: string;
  message: string;
}

/**
 * Enregistre un nouveau webhook
 * @param data Données du webhook (URL et événements)
 * @returns Le webhook créé
 */
export async function registerWebhook(
  data: RegisterWebhookRequest,
): Promise<Webhook> {
  const response = await apiClient.post<Webhook>('/webhooks', data);
  return response.data;
}

/**
 * Liste les webhooks de l'utilisateur authentifié
 * @returns Liste des webhooks
 */
export async function listWebhooks(): Promise<Webhook[]> {
  const response = await apiClient.get<Webhook[]>('/webhooks');
  return response.data;
}

/**
 * Supprime un webhook
 * @param id ID du webhook à supprimer
 * @returns Message de confirmation
 */
export async function deleteWebhook(id: string): Promise<{ message: string }> {
  const response = await apiClient.delete<{ message: string }>(
    `/webhooks/${id}`,
  );
  return response.data;
}

/**
 * Récupère les statistiques des webhooks
 * @returns Statistiques du worker de webhooks
 */
export async function getWebhookStats(): Promise<WebhookStatsResponse> {
  const response = await apiClient.get<WebhookStatsResponse>('/webhooks/stats');
  return response.data;
}

/**
 * Teste un webhook en envoyant un événement de test
 * @param id ID du webhook à tester
 * @returns Message de confirmation
 */
export async function testWebhook(id: string): Promise<{ message: string }> {
  const response = await apiClient.post<{ message: string }>(
    `/webhooks/${id}/test`,
  );
  return response.data;
}

/**
 * Régénère la clé API d'un webhook
 * @param id ID du webhook
 * @returns Nouvelle clé API et message de confirmation
 */
export async function regenerateWebhookAPIKey(
  id: string,
): Promise<RegenerateAPIKeyResponse> {
  const response = await apiClient.post<RegenerateAPIKeyResponse>(
    `/webhooks/${id}/regenerate-key`,
  );
  return response.data;
}
