import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '@environments/environment';
import { Observable } from 'rxjs';

export interface WebhookEvent {
  id: string;
  url: string;
  eventType: string;
  payload: any;
  status: 'SUCCESS' | 'FAILED' | 'PENDING';
  retryCount: number;
  createdAt: Date;
  lastAttempt?: Date;
  errorMessage?: string;
}

export interface WebhookFilter {
  status?: string;
  eventType?: string;
  startDate?: Date;
  endDate?: Date;
}

@Injectable({ providedIn: 'root' })
export class WebhookService {
  private http = inject(HttpClient);
  private apiUrl = `${environment.localServiceTestApiUrl}/webhooks`;

  getWebhookHistory(filters?: WebhookFilter): Observable<WebhookEvent[]> {
    return this.http.post<WebhookEvent[]>(
      `${this.apiUrl}/history`,
      filters
    );
  }

  getWebhookStatus(): Observable<{
    total: number;
    success: number;
    failed: number;
    pending: number;
  }> {
    return this.http.get<any>(`${this.apiUrl}/status`);
  }
}