<?php

declare(strict_types=1);

namespace App\ApiClient;

use DateTimeImmutable;

/**
 * Tracks the most recent ApiClient call's success/failure for the
 * UI's `/healthz` payload.
 *
 * SPEC §M08.8: a background ticker is overkill — just remember the
 * last call result. Healthz returns 200 even when the api is down so
 * orchestrators don't kill the UI just because the api is briefly
 * unreachable; the body carries `api_reachable` and `last_api_check_at`.
 */
final class ApiHealth
{
    private ?bool $reachable = null;
    private ?DateTimeImmutable $lastSuccessAt = null;

    public function recordSuccess(DateTimeImmutable $at): void
    {
        $this->reachable = true;
        $this->lastSuccessAt = $at;
    }

    public function recordFailure(): void
    {
        $this->reachable = false;
    }

    public function isReachable(): ?bool
    {
        return $this->reachable;
    }

    public function lastSuccessAt(): ?DateTimeImmutable
    {
        return $this->lastSuccessAt;
    }
}
