<?php

declare(strict_types=1);

namespace App\Framework\Console\Health;

final readonly class HealthReport
{
    /**
     * @param HealthStatus $overallStatus
     * @param array<string, HealthCheckResult> $results
     */
    public function __construct(
        public HealthStatus $overallStatus,
        public array $results
    ) {
    }

    public function isHealthy(): bool
    {
        return $this->overallStatus === HealthStatus::HEALTHY;
    }

    public function hasWarnings(): bool
    {
        return $this->overallStatus === HealthStatus::WARNING;
    }

    public function isUnhealthy(): bool
    {
        return $this->overallStatus === HealthStatus::UNHEALTHY;
    }

    public function getHealthyChecks(): array
    {
        return array_filter($this->results, fn ($result) => $result->isHealthy());
    }

    public function getWarningChecks(): array
    {
        return array_filter($this->results, fn ($result) => $result->isWarning());
    }

    public function getUnhealthyChecks(): array
    {
        return array_filter($this->results, fn ($result) => $result->isUnhealthy());
    }

    public function getCheckCount(): int
    {
        return count($this->results);
    }

    public function getHealthyCount(): int
    {
        return count($this->getHealthyChecks());
    }

    public function getWarningCount(): int
    {
        return count($this->getWarningChecks());
    }

    public function getUnhealthyCount(): int
    {
        return count($this->getUnhealthyChecks());
    }

    public function toArray(): array
    {
        return [
            'overall_status' => $this->overallStatus->value,
            'total_checks' => $this->getCheckCount(),
            'healthy_checks' => $this->getHealthyCount(),
            'warning_checks' => $this->getWarningCount(),
            'unhealthy_checks' => $this->getUnhealthyCount(),
            'results' => array_map(fn ($result) => $result->toArray(), $this->results),
        ];
    }
}
