<?php

declare(strict_types=1);

namespace App\Framework\Search;

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

/**
 * Statistics for a specific search index
 */
final readonly class SearchIndexStats
{
    public function __construct(
        public string $entityType,
        public int $documentCount,
        public Byte $indexSize,
        public int $queryCount,
        public float $averageQueryTimeMs,
        public Timestamp $lastIndexed,
        public Timestamp $createdAt,
        public array $metadata = []
    ) {
    }

    public function hasDocuments(): bool
    {
        return $this->documentCount > 0;
    }

    public function hasQueries(): bool
    {
        return $this->queryCount > 0;
    }

    public function getAverageDocumentSize(): float
    {
        if ($this->documentCount === 0) {
            return 0.0;
        }

        return $this->indexSize->toBytes() / $this->documentCount;
    }

    public function toArray(): array
    {
        return [
            'entity_type' => $this->entityType,
            'document_count' => $this->documentCount,
            'index_size_bytes' => $this->indexSize->toBytes(),
            'index_size_mb' => $this->indexSize->toMegaBytes(),
            'index_size_gb' => $this->indexSize->toGigaBytes(),
            'query_count' => $this->queryCount,
            'average_query_time_ms' => $this->averageQueryTimeMs,
            'average_document_size_bytes' => $this->getAverageDocumentSize(),
            'last_indexed' => $this->lastIndexed->toISOString(),
            'created_at' => $this->createdAt->toISOString(),
            'metadata' => $this->metadata,
        ];
    }
}
