<?php

declare(strict_types=1);

namespace App\Framework\Database\Cache\ValueObjects;

use App\Framework\Core\ValueObjects\Percentage;

/**
 * Entity-specific cache metrics value object
 */
final readonly class EntityCacheMetrics
{
    public function __construct(
        public int $hits,
        public int $misses,
        public Percentage $hitRatio,
        public int $puts,
        public int $evictions
    ) {
    }

    public function getTotalRequests(): int
    {
        return $this->hits + $this->misses;
    }

    public function getEvictionRate(): Percentage
    {
        return $this->puts > 0
            ? Percentage::fromRatio($this->evictions, $this->puts)
            : Percentage::zero();
    }

    public function toArray(): array
    {
        return [
            'hits' => $this->hits,
            'misses' => $this->misses,
            'hit_ratio' => $this->hitRatio->format(),
            'puts' => $this->puts,
            'evictions' => $this->evictions,
            'eviction_rate' => $this->getEvictionRate()->format(),
            'total_requests' => $this->getTotalRequests(),
        ];
    }
}
