import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { AnalyticsEvent } from '../models/analytics-event.model';
import { AnalyticsGateway } from './analytics-gateway.interface';

/** Contract: docs/backend/TRACK-A-ANALYTICS-CONTRACT.md §1 - server-side batched ingest. */
@Injectable({ providedIn: 'root' })
export class AnalyticsApiGateway implements AnalyticsGateway {
  private readonly http = inject(HttpClient);

  track(event: AnalyticsEvent): Observable<void> {
    return this.http
      .post('/api/v2/storefront/analytics/events', event)
      .pipe(map(() => undefined));
  }
}
