import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { AdminMonitoringEvent, AdminMonitoringEventFilters, AdminQueue, AdminWebhookDelivery } from '../models/admin-monitoring.model';
import { AdminMonitoringGateway } from './admin-monitoring-gateway.interface';

/**
 * No dedicated monitoring contract doc exists yet - at the conventional
 * /api/admin/v2/{resource} path per the interface's own doc comment
 * (a future HTTP gateway implements the same shape).
 */
@Injectable({ providedIn: 'root' })
export class AdminMonitoringApiGateway implements AdminMonitoringGateway {
  private readonly http = inject(HttpClient);

  loadEvents(filters: AdminMonitoringEventFilters): Observable<AdminMonitoringEvent[]> {
    let params = new HttpParams();
    for (const [key, value] of Object.entries(filters ?? {})) {
      if (value !== undefined && value !== null && value !== '') {
        params = params.set(key, String(value));
      }
    }
    return this.http.get<AdminMonitoringEvent[]>('/api/admin/v2/monitoring/events', { params });
  }

  loadQueues(): Observable<AdminQueue[]> {
    return this.http.get<AdminQueue[]>('/api/admin/v2/monitoring/queues');
  }

  loadWebhooks(): Observable<AdminWebhookDelivery[]> {
    return this.http.get<AdminWebhookDelivery[]>('/api/admin/v2/monitoring/webhooks');
  }
}
