import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { MarketplaceRevision } from '../models/marketplace-revision.model';
import { MarketplaceRevisionGateway } from './marketplace-revision-gateway.interface';

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

  createDraft(marketplaceId: string): Observable<MarketplaceRevision> {
    return this.http.post<MarketplaceRevision>(
      `/api/admin/v2/marketplaces/${encodeURIComponent(marketplaceId)}/revisions`,
      {},
    );
  }

  validate(marketplaceId: string, revisionId: string): Observable<MarketplaceRevision> {
    return this.http.post<MarketplaceRevision>(
      `/api/admin/v2/marketplaces/${encodeURIComponent(marketplaceId)}/revisions/${encodeURIComponent(revisionId)}/validate`,
      {},
    );
  }

  publish(marketplaceId: string, revisionId: string): Observable<MarketplaceRevision> {
    return this.http.post<MarketplaceRevision>(
      `/api/admin/v2/marketplaces/${encodeURIComponent(marketplaceId)}/revisions/${encodeURIComponent(revisionId)}/publish`,
      {},
    );
  }

  rollback(marketplaceId: string, revisionId: string): Observable<MarketplaceRevision> {
    return this.http.post<MarketplaceRevision>(
      `/api/admin/v2/marketplaces/${encodeURIComponent(marketplaceId)}/revisions/${encodeURIComponent(revisionId)}/rollback`,
      {},
    );
  }
}
