<?php

declare(strict_types=1);

namespace App\Framework\Search;

use App\Framework\Core\ValueObjects\Byte;
use App\Framework\Core\ValueObjects\Timestamp;

/**
 * Search engine statistics and health information
 */
final readonly class SearchEngineStats
{
    /**
     * @param array<string, int> $indexCounts
     * @param array<string, mixed> $metadata
     */
    public function __construct(
        public string $engineName,
        public string $version,
        public bool $isHealthy,
        public int $totalDocuments,
        public array $indexCounts,
        public Byte $diskUsage,
        public Byte $memoryUsage,
        public float $averageQueryTimeMs,
        public int $totalQueries,
        public Timestamp $lastUpdated,
        public array $metadata = []
    ) {
    }

    public function hasIndexes(): bool
    {
        return ! empty($this->indexCounts);
    }

    public function getIndexCount(): int
    {
        return count($this->indexCounts);
    }

    public function getDocumentsForIndex(string $index): int
    {
        return $this->indexCounts[$index] ?? 0;
    }

    public function toArray(): array
    {
        return [
            'engine_name' => $this->engineName,
            'version' => $this->version,
            'is_healthy' => $this->isHealthy,
            'total_documents' => $this->totalDocuments,
            'index_counts' => $this->indexCounts,
            'index_count' => $this->getIndexCount(),
            'disk_usage_bytes' => $this->diskUsage->toBytes(),
            'disk_usage_mb' => $this->diskUsage->toMegaBytes(),
            'disk_usage_gb' => $this->diskUsage->toGigaBytes(),
            'memory_usage_bytes' => $this->memoryUsage->toBytes(),
            'memory_usage_mb' => $this->memoryUsage->toMegaBytes(),
            'memory_usage_gb' => $this->memoryUsage->toGigaBytes(),
            'average_query_time_ms' => $this->averageQueryTimeMs,
            'total_queries' => $this->totalQueries,
            'last_updated' => $this->lastUpdated->toISOString(),
            'metadata' => $this->metadata,
        ];
    }
}
