# fitz_ai/ingest/enrichment/config.py
"""
Configuration schema for the enrichment pipeline.

Enrichment is an optional pipeline step that enhances chunks with:
1. Summaries - LLM-generated descriptions for better search (universal)
2. Artifacts - High-level project insights (type-specific plugins)

Config structure in .fitz/config.yaml:
    enrichment:
      enabled: true
      summary:
        enabled: false  # Opt-in: 1 LLM call per chunk
      artifacts:
        auto: true
        disabled: []
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass
class SummaryConfig:
    """
    Configuration for chunk-level summaries.

    Summaries are LLM-generated descriptions that improve search relevance.
    They are universal - applied to all chunk types using type-specific
    context builders for optimal results.

    WARNING: Summaries make 1 LLM call per chunk. For a codebase with 1000
    chunks, that's 1000 API calls. Enable explicitly only when needed.

    Attributes:
        enabled: Whether to generate summaries (default: False - opt-in)
        provider: LLM provider to use (defaults to config's chat provider)
        model: Model to use (defaults to config's chat model)
    """

    enabled: bool = False  # Opt-in: 1 LLM call per chunk is expensive
    provider: str | None = None
    model: str | None = None


@dataclass
class ArtifactConfig:
    """
    Configuration for project-level artifacts.

    Artifacts are high-level project summaries generated by plugins.
    Each plugin declares which content types it supports. Plugins are
    auto-discovered from the artifacts/plugins/ directory.

    Attributes:
        auto: Auto-discover and run all applicable plugins
        enabled: Explicit list of plugins to run (overrides auto)
        disabled: Plugins to skip even if applicable
    """

    auto: bool = True
    enabled: list[str] = field(default_factory=list)
    disabled: list[str] = field(default_factory=list)


@dataclass
class EnrichmentConfig:
    """
    Configuration for the enrichment pipeline.

    The enrichment pipeline is an optional step in document ingestion
    that enhances chunks and generates project-level insights.

    Attributes:
        enabled: Master switch for all enrichment
        summary: Chunk-level summary configuration
        artifacts: Project-level artifact configuration

    Example in .fitz/config.yaml:
        enrichment:
          enabled: true
          summary:
            enabled: true
          artifacts:
            auto: true
            disabled:
              - architecture_narrative
    """

    enabled: bool = False
    summary: SummaryConfig = field(default_factory=SummaryConfig)
    artifacts: ArtifactConfig = field(default_factory=ArtifactConfig)

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> "EnrichmentConfig":
        """Create config from dictionary (e.g., from YAML)."""
        if not data:
            return cls()

        summary_data = data.get("summary", {})
        artifacts_data = data.get("artifacts", {})

        return cls(
            enabled=data.get("enabled", False),
            summary=SummaryConfig(
                enabled=summary_data.get("enabled", False),  # Opt-in
                provider=summary_data.get("provider"),
                model=summary_data.get("model"),
            ),
            artifacts=ArtifactConfig(
                auto=artifacts_data.get("auto", True),
                enabled=artifacts_data.get("enabled", []),
                disabled=artifacts_data.get("disabled", []),
            ),
        )

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary for serialization."""
        return {
            "enabled": self.enabled,
            "summary": {
                "enabled": self.summary.enabled,
                "provider": self.summary.provider,
                "model": self.summary.model,
            },
            "artifacts": {
                "auto": self.artifacts.auto,
                "enabled": self.artifacts.enabled,
                "disabled": self.artifacts.disabled,
            },
        }


__all__ = [
    "EnrichmentConfig",
    "SummaryConfig",
    "ArtifactConfig",
]
