import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { LifecycleAdvanceResult, Marketplace, MarketplaceDomain } from '../models/marketplace.model';
import { MarketplaceGateway } from './marketplace-gateway.interface';

/** Contract: docs/backend/PHASE-9-TENANT-REGISTRY-DOMAINS-CONTRACT.md §2-4. */
@Injectable({ providedIn: 'root' })
export class MarketplaceApiGateway implements MarketplaceGateway {
  private readonly http = inject(HttpClient);

  loadMarketplaces(): Observable<Marketplace[]> {
    // The contract specifies POST/PATCH marketplace endpoints in detail but
    // not the list GET explicitly - inferred at the collection root, the
    // conventional counterpart to POST /api/admin/v2/marketplaces (§3 step 1).
    return this.http.get<Marketplace[]>('/api/admin/v2/marketplaces');
  }

  loadDomains(marketplaceId: string): Observable<MarketplaceDomain[]> {
    return this.http.get<MarketplaceDomain[]>(
      `/api/admin/v2/marketplaces/${encodeURIComponent(marketplaceId)}/domains`,
    );
  }

  loadLifecycle(marketplaceId: string): Observable<LifecycleAdvanceResult> {
    return this.http.get<LifecycleAdvanceResult>(
      `/api/admin/v2/marketplaces/${encodeURIComponent(marketplaceId)}/lifecycle`,
    );
  }
}
