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

export interface MerchantsConfig {
  apiKey: string;
  secretKey: string;
  webhookUrl: string;
  callbackUrls: {
    paymentSuccess: string;
    paymentFailed: string;
    subscriptionCreated: string;
    subscriptionCancelled: string;
  };
  allowedIPs: string[];
  rateLimit: number;
  isActive: boolean;
}

@Injectable({ providedIn: 'root' })
export class MerchantsConfigService {
  private http = inject(HttpClient);

  getConfig(merchantId: string): Observable<MerchantsConfig> {
    return this.http.get<MerchantsConfig>(
      `${environment.apiUrl}/merchants/${merchantId}/config`
    );
  }

  updateConfig(merchantId: string, config: MerchantsConfig): Observable<MerchantsConfig> {
    return this.http.put<MerchantsConfig>(
      `${environment.apiUrl}/merchants/${merchantId}/config`,
      config
    );
  }

  regenerateApiKey(merchantId: string): Observable<{ apiKey: string }> {
    return this.http.post<{ apiKey: string }>(
      `${environment.apiUrl}/merchants/${merchantId}/regenerate-key`,
      {}
    );
  }

  testWebhook(merchantId: string): Observable<{ success: boolean; message: string }> {
    return this.http.post<{ success: boolean; message: string }>(
      `${environment.apiUrl}/merchants/${merchantId}/test-webhook`,
      {}
    );
  }
}