<?php

declare(strict_types=1);

namespace App\Index;

use App\Repository\IngestProfileRepository;

final class IndexConfigurationProvider
{
    private IngestProfileRepository $repository;

    private int $fallbackChunkSize;
    private int $fallbackChunkOverlap;
    private string $fallbackEmbeddingModel;
    private int $fallbackEmbeddingDimension;
    private int $fallbackScoringVersion;

    public function __construct(
        IngestProfileRepository $repository,
        int $fallbackChunkSize,
        int $fallbackChunkOverlap,
        string $fallbackEmbeddingModel,
        int $fallbackEmbeddingDimension,
        int $fallbackScoringVersion
    ) {
        $this->repository = $repository;

        $this->fallbackChunkSize = $fallbackChunkSize;
        $this->fallbackChunkOverlap = $fallbackChunkOverlap;
        $this->fallbackEmbeddingModel = $fallbackEmbeddingModel;
        $this->fallbackEmbeddingDimension = $fallbackEmbeddingDimension;
        $this->fallbackScoringVersion = $fallbackScoringVersion;
    }

    public function getConfiguration(): IndexConfiguration
    {
        $active = $this->repository->findActive();

        if ($active === null) {
            // Fallback auf YAML
            return new IndexConfiguration(
                $this->fallbackChunkSize,
                $this->fallbackChunkOverlap,
                $this->fallbackEmbeddingModel,
                $this->fallbackEmbeddingDimension,
                $this->fallbackScoringVersion,
                'ndjson',
                'faiss'
            );
        }

        return new IndexConfiguration(
            $active->getChunkSize(),
            $active->getChunkOverlap(),
            $active->getEmbeddingModel(),
            $active->getEmbeddingDimension(),
            $active->getScoringVersion(),
            'ndjson',
            'faiss'
        );
    }
}
