<?php


declare(strict_types=1);

namespace App\Index;

/**
 * Beschreibt die "Struktur" des Index (nicht den Inhalt).
 * Diese Werte müssen bei lokalem Ingest mit index_meta.json kompatibel sein,
 * sonst muss ein Global Reindex erzwungen werden.
 */
final class IndexConfiguration
{
    public function __construct(
        private readonly int    $chunkSize,
        private readonly int    $chunkOverlap,
        private readonly string $embeddingModel,
        private readonly int    $embeddingDimension,
        private readonly int    $scoringVersion,
        private readonly string $indexFormat = 'ndjson',   // bindend: 'ndjson'
        private readonly string $vectorBackend = 'faiss',  // informativ
    )
    {
        if ($this->chunkSize <= 0) {
            throw new \InvalidArgumentException('chunkSize must be > 0');
        }
        if ($this->chunkOverlap < 0) {
            throw new \InvalidArgumentException('chunkOverlap must be >= 0');
        }
        if ($this->chunkOverlap >= $this->chunkSize) {
            throw new \InvalidArgumentException('chunkOverlap must be < chunkSize');
        }
        if ($this->embeddingDimension <= 0) {
            throw new \InvalidArgumentException('embeddingDimension must be > 0');
        }
        if ($this->scoringVersion <= 0) {
            throw new \InvalidArgumentException('scoringVersion must be > 0');
        }
        if ($this->indexFormat !== 'ndjson') {
            throw new \InvalidArgumentException('indexFormat must be "ndjson"');
        }
    }

    public function getChunkSize(): int
    {
        return $this->chunkSize;
    }

    public function getChunkOverlap(): int
    {
        return $this->chunkOverlap;
    }

    public function getEmbeddingModel(): string
    {
        return $this->embeddingModel;
    }

    public function getEmbeddingDimension(): int
    {
        return $this->embeddingDimension;
    }

    public function getScoringVersion(): int
    {
        return $this->scoringVersion;
    }

    public function getIndexFormat(): string
    {
        return $this->indexFormat;
    }

    public function getVectorBackend(): string
    {
        return $this->vectorBackend;
    }

    /**
     * Canonical representation: nur strukturelle Felder (ohne created_at, index_version).
     */
    public function toStructureArray(): array
    {
        return [
            'embedding_model' => $this->embeddingModel,
            'embedding_dimension' => $this->embeddingDimension,
            'chunk_size' => $this->chunkSize,
            'chunk_overlap' => $this->chunkOverlap,
            'scoring_version' => $this->scoringVersion,
            'index_format' => $this->indexFormat,
            'vector_backend' => $this->vectorBackend,
        ];
    }
}
