import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Customer } from '../models/customer-identity.model';
import { VkIdGateway } from './vk-id-gateway.interface';

/**
 * Contract: docs/backend/PHASE-8-IDENTITY-MESSAGING-CONTRACT.md §2.
 * OAuth completion happens backend-side; this is the client-facing surface
 * only - getAuthorizeUrl navigates the browser there, completeCallback hands
 * back the code/verifier pair for the backend to exchange.
 */
@Injectable({ providedIn: 'root' })
export class VkIdApiGateway implements VkIdGateway {
  private readonly http = inject(HttpClient);

  getAuthorizeUrl(): Observable<string> {
    return this.http
      .get<{ url: string }>('/api/identity/v1/vk/authorize')
      .pipe(map(response => response.url));
  }

  completeCallback(code: string, codeVerifier: string): Observable<Customer> {
    return this.http.post<Customer>('/api/identity/v1/vk/callback', { code, codeVerifier });
  }
}
