"""SDK configuration extending ``src.config.Settings``."""

from __future__ import annotations

from pathlib import Path
from typing import Literal, Optional

from pydantic import Field
from pydantic_settings import BaseSettings

GraphFallbackPolicy = Literal["strict", "best_effort", "off"]
KnowledgeBackend = Literal["legacy", "ike", "hybrid"]


class SDKConfig(BaseSettings):
    """SDK-specific settings layered on top of DataBridge core config.

    Loads from environment variables with ``SDK_`` prefix, falling back
    to ``.env`` file.  Core DataBridge settings (``data_dir``,
    ``persistence_backend``, etc.) are inherited at runtime by importing
    ``src.config.settings`` directly — this class only holds SDK-layer
    additions.

    Attributes:
        sdk_data_dir: Root directory for SDK artifacts (audit logs,
            compiled DDL, workflow state).
        audit_log_path: Path to the SDK audit CSV.
        snowflake_connection_name: Named connection in
            ``~/.snowflake/config.toml`` to use by default.
        compiler_output_dir: Where the Snowflake compiler writes DDL.
        enable_agent_assist: Whether ``SolutionBuilder.design()`` uses LLM.
        spcs_endpoint: URL of the SPCS-resident DataBridge instance.
        default_compile_target: Default Snowflake compilation target
            (``dynamic_table``, ``task``, ``stored_proc``).
    """

    sdk_data_dir: Path = Field(
        default=Path("data/sdk"),
        description="Root directory for SDK artifacts.",
    )
    audit_log_path: Path = Field(
        default=Path("data/sdk/audit.csv"),
        description="SOX-compliant audit log path.",
    )
    snowflake_connection_name: Optional[str] = Field(
        default=None,
        description="Named Snowflake connection from config.toml.",
    )
    compiler_output_dir: Path = Field(
        default=Path("data/sdk/compiled"),
        description="Output directory for compiled Snowflake DDL.",
    )
    enable_agent_assist: bool = Field(
        default=False,
        description="Enable LLM-powered design assistance.",
    )
    spcs_endpoint: Optional[str] = Field(
        default=None,
        description="SPCS DataBridge endpoint URL.",
    )
    enable_graphs: bool = Field(
        default=True,
        description="Master switch for graph integration. Set False to disable all graph systems.",
    )
    enable_lineage: bool = Field(
        default=True,
        description="Enable lineage tracking (data provenance DAG).",
    )
    enable_evidence: bool = Field(
        default=True,
        description="Enable evidence trail (auditable mapping decisions).",
    )
    enable_vectors: bool = Field(
        default=True,
        description="Enable GraphRAG vector store indexing (semantic search).",
    )
    knowledge_backend: KnowledgeBackend = Field(
        default="hybrid",
        description="Canonical durable knowledge routing mode: 'legacy', 'ike', or 'hybrid'.",
    )
    legacy_graph_vector_enabled: bool = Field(
        default=True,
        description="Allow GraphBridge to initialize and mirror legacy GraphRAG/KB storage backends.",
    )
    enable_kb_graph: bool = Field(
        default=True,
        description="Enable knowledge base graph ingestion.",
    )
    enable_entity_linker: bool = Field(
        default=True,
        description="Enable entity linker (BLCE entity clustering).",
    )
    enable_codex: bool = Field(
        default=True,
        description="Enable Codex GraphRAG (runtime discrepancy events).",
    )
    enable_ike_graph: bool = Field(
        default=False,
        description="Enable IKE Graph runtime integration.",
    )
    enable_formula_graph: bool = Field(
        default=False,
        description="Enable Formula Dependency Graph runtime integration via IKE Excel APIs.",
    )
    lineage_output_dir: Optional[str] = Field(
        default=None,
        description="Lineage tracker output directory (defaults to data/lineage).",
    )
    kb_base_dir: Optional[str] = Field(
        default=None,
        description="Knowledge base directory (defaults to data/knowledge_base).",
    )
    enable_ike: bool = Field(
        default=False,
        description="Enable IKE (Institutional Knowledge Engine) integration.",
    )
    ike_endpoint: Optional[str] = Field(
        default=None,
        description="IKE REST API endpoint (e.g. http://localhost:8000).",
    )
    ike_tenant_id: str = Field(
        default="default",
        description="IKE tenant identifier for multi-tenant isolation.",
    )
    graph_fallback_policy: str = Field(
        default="best_effort",
        description="Graph integration failure policy: 'strict' (raise on error), "
        "'best_effort' (log and continue), 'off' (skip graph entirely).",
    )
    graph_query_cache_ttl: int = Field(
        default=300,
        description="TTL in seconds for per-run graph query cache entries.",
    )
    default_compile_target: str = Field(
        default="dynamic_table",
        description="Default Snowflake object type for compilation.",
    )

    model_config = {
        "env_prefix": "SDK_",
        "env_file": ".env",
        "env_file_encoding": "utf-8",
        "extra": "ignore",
    }
