import { Injectable, inject } from '@angular/core';
import { Observable, forkJoin, map } from 'rxjs';
import { BackofficeDataService } from '../../../../core/backoffice/backoffice-data.service';
import { AdminDashboardMetrics } from '../models/admin-dashboard.model';
import { AdminDashboardMetricsGateway } from './admin-dashboard-metrics.gateway.interface';

/** Local composition of existing backoffice data sources. Replace with an HTTP gateway once the backend exposes dashboard metrics endpoints - the facade contract stays the same. */
@Injectable({ providedIn: 'root' })
export class AdminDashboardMetricsLocalGateway implements AdminDashboardMetricsGateway {
  private readonly backofficeData = inject(BackofficeDataService);

  loadMetrics(): Observable<AdminDashboardMetrics> {
    return forkJoin({
      categories: this.backofficeData.loadCategories(),
      products: this.backofficeData.loadProducts(),
    }).pipe(
      map(({ categories, products }) => ({
        categoriesCount: categories.length,
        productsCount: products.length,
      })),
    );
  }
}
