<?php

declare(strict_types=1);

namespace App\Framework\Database\Cache\ValueObjects;

use App\Framework\Core\ValueObjects\Percentage;

/**
 * Cache performance metrics value object
 */
final readonly class CachePerformanceMetrics
{
    public function __construct(
        public EntityCacheMetrics $entityCache,
        public QueryCacheMetrics $queryCache,
        public MemoryUsageMetrics $memory
    ) {
    }

    public function getOverallHitRatio(): Percentage
    {
        $totalHits = $this->entityCache->hits + $this->queryCache->hits;
        $totalMisses = $this->entityCache->misses + $this->queryCache->misses;

        return ($totalHits + $totalMisses) > 0
            ? Percentage::fromRatio($totalHits, $totalHits + $totalMisses)
            : Percentage::zero();
    }

    public function toArray(): array
    {
        return [
            'entity_cache' => $this->entityCache->toArray(),
            'query_cache' => $this->queryCache->toArray(),
            'memory' => $this->memory->toArray(),
            'overall_hit_ratio' => $this->getOverallHitRatio()->format(),
        ];
    }
}
