import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { type BackendHealth, HomeStatusService } from './home-status.service';

/**
 * Landing page at `/`. Replaces the Nx-generated welcome scaffold.
 *
 * v1 carries:
 *   - A welcome heading + brief description (placeholder content
 *     until the comms team / a11y panel reviews and lands the real
 *     copy).
 *   - A "system status" widget that hits the BFF's `/api/health`.
 *     Doubles as a smoke test of the full SPA → BFF stack: CORS,
 *     OTel trace propagation, Pino log correlation. After page
 *     load, http://localhost:16686 should show one trace whose
 *     root span is the SPA `document_load`, with a child `fetch`
 *     span that has its own child `HTTP GET /api/health` BFF span.
 */
@Component({
  selector: 'app-home',
  templateUrl: './home.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class Home {
  private readonly statusService = inject(HomeStatusService);

  // The discriminated `HomeStatus` does not narrow cleanly through
  // Angular's template type-checker via `@let`/`@if`, so we expose
  // three pre-narrowed signals the template can read directly.
  protected readonly isLoading = computed(() => this.statusService.status().state === 'loading');
  protected readonly isError = computed(() => this.statusService.status().state === 'error');
  protected readonly health = computed<BackendHealth | null>(() => {
    const s = this.statusService.status();
    return s.state === 'ok' ? s.health : null;
  });
}
