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

export interface Operator {
  id: string;
  name: string;
  country: string;
  status: 'ACTIVE' | 'INACTIVE';
  config: OperatorConfig;
}

export interface OperatorConfig {
  apiEndpoint: string;
  apiKey: string;
  secretKey: string;
  timeout: number;
  retryAttempts: number;
  webhookUrl: string;
  isActive: boolean;
  supportedServices: string[];
}

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

  getOperators(): Observable<Operator[]> {
    return this.http.get<Operator[]>(`${this.apiUrl}`);
  }

  updateOperatorConfig(operatorId: string, config: OperatorConfig): Observable<Operator> {
    return this.http.put<Operator>(
      `${this.apiUrl}/${operatorId}/config`,
      config
    );
  }

  testConnection(operatorId: string): Observable<{ success: boolean; latency: number }> {
    return this.http.post<{ success: boolean; latency: number }>(
      `${this.apiUrl}/${operatorId}/test-connection`,
      {}
    );
  }
}