# ------------------------------------------------------------
# Parameters
# ------------------------------------------------------------

parameters:
  mto.root: '%kernel.project_dir%'
  mto.kernel.dir: '%mto.root%'
  mto.locks.dir: '%mto.knowledge.root%/locks'

  mto.knowledge.root: '%mto.root%/var/knowledge'

  mto.knowledge.ndjson: '%mto.knowledge.root%/index.ndjson'
  mto.knowledge.index_meta: '%mto.knowledge.root%/index_meta.json'
  mto.knowledge.vector_index: '%mto.knowledge.root%/vector.index'
  mto.knowledge.vector_index_meta: '%mto.knowledge.root%/vector.index.meta.json'
  mto.runtime.meta: '%mto.knowledge.root%/index_runtime.json'
  mto.knowledge.upload: '%mto.knowledge.root%/uploads'

  mto.knowledge.tags_ndjson: '%mto.knowledge.root%/tags.ndjson'
  mto.knowledge.vector_tags_index: '%mto.knowledge.root%/vector_tags.index'
  mto.knowledge.vector_tags_index_meta: '%mto.knowledge.root%/vector_tags.index.meta.json'

  mto.vector.script_dir: '%mto.root%/python/vector'

  mto.vector.ingest_tags_script: '%mto.vector.script_dir%/vector_ingest_tags.py'
  mto.vector.search_tags_script: '%mto.vector.script_dir%/vector_search_tags.py'

  mto.tags.rebuild_lock: '%mto.locks.dir%/tag_rebuild.lock'

  mto.vector.data.upload.path: '%mto.knowledge.upload%'

  mto.index.chunk_size: 800
  mto.index.chunk_overlap: 100
  mto.index.embedding_model: 'intfloat/multilingual-e5-base'
  mto.index.embedding_dimension: 768
  mto.index.scoring_version: 1

  mto.vector.python_bin: '%kernel.project_dir%/.venv/bin/python3'
  mto.vector.ingest_script: '%mto.vector.script_dir%/vector_ingest.py'
  mto.vector.search_script: '%mto.vector.script_dir%/vector_search.py'
  mto.vector.timeout: 600
  mto.vector.service_url: 'http://127.0.0.1:8090'

  mto.commerce.enabled: true
  mto.commerce.max_shop_results: '%env(SHOPWARE_STORE_API_MAX_RESULT)%'
  mto.commerce.shop_timeout: 5
  mto.commerce.store_api_base_url: '%env(SHOPWARE_STORE_API_BASE_URL)%'
  mto.commerce.sales_channel_access_key: '%env(SHOPWARE_SALES_CHANNEL_ACCESS_KEY)%'

  mto.commerce.search_repair.enabled: true
  mto.commerce.search_repair.max_queries: 3
  mto.commerce.search_repair.min_primary_results_without_repair: 2


# ------------------------------------------------------------
# Services
# ------------------------------------------------------------

services:

  _defaults:
    autowire: true
    autoconfigure: true
    bind:
      Psr\Log\LoggerInterface $agentLogger: '@monolog.logger.agent'
      string $projectDir: '%mto.root%'

  App\:
    resource: '../src/'
    exclude:
      - '../src/DependencyInjection/'
      - '../src/Entity/'
      - '../src/Kernel.php'

  App\Controller\:
    resource: '../src/Controller/'
    tags: [ 'controller.service_arguments' ]

  # ------------------------------------------------------------
  # AI Agent – Infrastructure
  # ------------------------------------------------------------

  App\Infrastructure\OllamaClient:
    arguments:
      $apiUrl: '%env(AI_LLM_API_URL)%'
      $timeoutSeconds: 600
      $configProvider: '@App\Service\ModelGenerationConfigProvider'

  # ------------------------------------------------------------
  # AI Agent – Context & Runner
  # ------------------------------------------------------------

  App\Context\ContextService:
    arguments:
      $historyDir: '%env(AI_HISTORY_DIR)%'
      $projectDir: '%mto.root%'

  App\Agent\AgentRunner:
    arguments:
      $debug: '%env(bool:AI_DEBUG)%'
      $logPrompt: '%env(bool:AI_LOG_PROMPT)%'
      $logContext: '%env(bool:AI_LOG_CONTEXT)%'

  # ------------------------------------------------------------
  # NDJSON Retrieval Stack
  # ------------------------------------------------------------

  App\Knowledge\Retrieval\NdjsonChunkLookup: ~

  App\Knowledge\Retrieval\NdjsonHybridRetriever: ~

  App\Knowledge\Retrieval\RetrieverInterface:
    alias: App\Knowledge\Retrieval\NdjsonHybridRetriever

  # ------------------------------------------------------------
  # Commerce / Shopware Store API
  # ------------------------------------------------------------

  App\Intent\CommerceIntentLite: ~

  App\Commerce\CommerceQueryParser: ~

  App\Commerce\SearchRepairService:
    arguments:
      $logger: '@monolog.logger.agent'
      $enabled: '%mto.commerce.search_repair.enabled%'
      $maxRepairQueries: '%mto.commerce.search_repair.max_queries%'
      $minPrimaryResultsWithoutRepair: '%mto.commerce.search_repair.min_primary_results_without_repair%'

  App\Shopware\ShopwareCriteriaBuilder: ~

  App\Shopware\StoreApiClient:
    arguments:
      $baseUrl: '%mto.commerce.store_api_base_url%'
      $salesChannelAccessKey: '%mto.commerce.sales_channel_access_key%'
      $timeoutSeconds: '%mto.commerce.shop_timeout%'

  App\Commerce\ShopSearchService:
    arguments:
      $enabled: '%mto.commerce.enabled%'
      $maxResults: '%mto.commerce.max_shop_results%'
      $baseUrl: '%mto.commerce.store_api_base_url%'

  # ------------------------------------------------------------
  # Index Configuration Provider
  # ------------------------------------------------------------

  App\Index\IndexConfigurationProvider:
    arguments:
      $repository: '@App\Repository\IngestProfileRepository'
      $fallbackChunkSize: '%mto.index.chunk_size%'
      $fallbackChunkOverlap: '%mto.index.chunk_overlap%'
      $fallbackEmbeddingModel: '%mto.index.embedding_model%'
      $fallbackEmbeddingDimension: '%mto.index.embedding_dimension%'
      $fallbackScoringVersion: '%mto.index.scoring_version%'

  # ------------------------------------------------------------
  # Index Meta Manager
  # ------------------------------------------------------------

  App\Index\IndexMetaManager:
    arguments:
      $metaPath: '%mto.knowledge.index_meta%'
      $runTimePath: '%mto.runtime.meta%'
      $provider: '@App\Index\IndexConfigurationProvider'

  # ------------------------------------------------------------
  # Vector Layer
  # ------------------------------------------------------------

  App\Vector\VectorSearchClient:
    arguments:
      $serviceUrl: '%mto.vector.service_url%'
      $agentLogger: '@monolog.logger.agent'

  App\Vector\VectorIndexBuilder:
    arguments:
      $pythonBin: '%mto.vector.python_bin%'
      $scriptPath: '%mto.vector.ingest_script%'
      $indexNdjsonPath: '%mto.knowledge.ndjson%'
      $vectorIndexPath: '%mto.knowledge.vector_index%'
      $timeoutSeconds: '%mto.vector.timeout%'
      $configurationProvider: '@App\Index\IndexConfigurationProvider'

  # ------------------------------------------------------------
  # Ingest Layer (Phase B Refactor)
  # ------------------------------------------------------------

  App\Ingest\GuardrailValidator: ~

  App\Ingest\ChunkWriteService: ~

  App\Ingest\VectorRebuildService: ~

  App\Ingest\IngestFlow: ~

  # ------------------------------------------------------------
  # Tags Export
  # ------------------------------------------------------------

  App\Tag\TagNdjsonExporter:
    arguments:
      $tagsNdjsonPath: '%mto.knowledge.tags_ndjson%'

  # ------------------------------------------------------------
  # Tags Vector
  # ------------------------------------------------------------

  App\Tag\TagVectorIndexBuilder:
    arguments:
      $pythonBin: '%mto.vector.python_bin%'
      $scriptPath: '%mto.vector.ingest_tags_script%'
      $tagsNdjsonPath: '%mto.knowledge.tags_ndjson%'
      $vectorTagsIndexPath: '%mto.knowledge.vector_tags_index%'
      $embeddingModel: '%mto.index.embedding_model%'
      $timeoutSeconds: '%mto.vector.timeout%'
      $agentLogger: '@monolog.logger.agent'

  App\Tag\TagVectorSearchClient:
    arguments:
      $serviceUrl: '%mto.vector.service_url%'
      $agentLogger: '@monolog.logger.agent'

  App\Tag\TagRoutingService: ~

  App\Tag\TagVectorIndexHealthService:
    arguments:
      $tagsNdjsonPath: '%mto.knowledge.tags_ndjson%'
      $vectorTagsIndexPath: '%mto.knowledge.vector_tags_index%'
      $vectorTagsMetaPath: '%mto.knowledge.vector_tags_index_meta%'

  # ------------------------------------------------------------
  # Tag Rebuild Jobs
  # ------------------------------------------------------------

  App\Service\TagRebuildJobService:
    arguments:
      $projectDir: '%mto.root%'
      $agentLogger: '@monolog.logger.agent'

  App\Command\TagRebuildRunJobCommand:
    arguments:
      $lockFilePath: '%mto.tags.rebuild_lock%'

  # ------------------------------------------------------------
  # Admin Utilities
  # ------------------------------------------------------------

  App\Service\Admin\IndexNdjsonInspector:
    arguments:
      $ndJsonPath: '%mto.knowledge.ndjson%'
      $indexMetaPath: '%mto.knowledge.index_meta%'

  App\Vector\VectorIndexHealthService:
    arguments:
      $indexNdjsonPath: '%mto.knowledge.ndjson%'
      $vectorIndexPath: '%mto.knowledge.vector_index%'
      $vectorMetaPath: '%mto.knowledge.vector_index_meta%'