import apiClient from '../../../shared/services/apiClient';

export interface Webhook {
  id: number;
  name: string;
  url: string;
  events: string[];
  status: 'active' | 'inactive' | 'paused';
  secret?: string;
  retry_count: number;
  timeout_seconds: number;
  description?: string;
  created_at: string;
}

export interface WebhookDelivery {
  id: number;
  webhook_id: number;
  event_type: string;
  event_id: string;
  status: 'pending' | 'success' | 'failed' | 'retrying';
  response_status?: number;
  attempt_count: number;
  created_at: string;
  delivered_at?: string;
}

export interface CreateWebhookData {
  name: string;
  url: string;
  events: string[];
  description?: string;
  retry_count?: number;
  timeout_seconds?: number;
}

export interface UpdateWebhookData {
  name?: string;
  url?: string;
  events?: string[];
  description?: string;
  status?: string;
  retry_count?: number;
  timeout_seconds?: number;
}

class WebhookService {
  async createWebhook(data: CreateWebhookData) {
    const response = await apiClient.post('/webhooks', data);
    return response.data;
  }

  async getWebhooks() {
    const response = await apiClient.get('/webhooks');
    return response.data;
  }

  async updateWebhook(webhookId: number, data: UpdateWebhookData) {
    const response = await apiClient.put(`/webhooks/${webhookId}`, data);
    return response.data;
  }

  async deleteWebhook(webhookId: number) {
    const response = await apiClient.delete(`/webhooks/${webhookId}`);
    return response.data;
  }

  async getWebhookDeliveries(webhookId: number, page: number = 1, limit: number = 50) {
    const response = await apiClient.get(`/webhooks/${webhookId}/deliveries`, {
      params: { page, limit },
    });
    return response.data;
  }
}

export default new WebhookService();

