<?php

declare(strict_types=1);

namespace App\Framework\Database\Cache\ValueObjects;

use App\Framework\Core\ValueObjects\Duration;
use App\Framework\Core\ValueObjects\Percentage;

/**
 * Individual entity metrics value object
 */
final readonly class EntityMetrics
{
    public function __construct(
        public int $hits,
        public int $misses,
        public Percentage $hitRatio,
        public int $cacheSize,
        public Duration $averageAccessTime
    ) {
    }

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

    public function isPopular(): bool
    {
        return $this->getTotalAccess() > 100; // Arbitrary threshold
    }

    public function isEfficient(): bool
    {
        return $this->hitRatio->isAbove(Percentage::from(60.0));
    }

    public function toArray(): array
    {
        return [
            'hits' => $this->hits,
            'misses' => $this->misses,
            'hit_ratio' => $this->hitRatio->format(),
            'cache_size' => $this->cacheSize,
            'average_access_time_ms' => $this->averageAccessTime->toMilliseconds(),
            'total_access' => $this->getTotalAccess(),
            'is_popular' => $this->isPopular(),
            'is_efficient' => $this->isEfficient(),
        ];
    }
}
