import logging

from app.core.config import Settings

logger = logging.getLogger(__name__)


def validate_runtime_settings(settings: Settings) -> None:
    errors = []

    if settings.GRAPHRAG_ENABLED:
        if settings.GRAPHRAG_GRAPH_STORAGE.lower() not in {"neo4j", "networkx"}:
            errors.append("GRAPHRAG_GRAPH_STORAGE must be either 'neo4j' or 'networkx'.")

        if settings.GRAPHRAG_GRAPH_STORAGE.lower() == "neo4j":
            if not settings.NEO4J_URL:
                errors.append("NEO4J_URL is required when GraphRAG Neo4j storage is enabled.")
            if not settings.NEO4J_USERNAME:
                errors.append("NEO4J_USERNAME is required when GraphRAG Neo4j storage is enabled.")
            if not settings.NEO4J_PASSWORD:
                errors.append("NEO4J_PASSWORD is required when GraphRAG Neo4j storage is enabled.")

    if settings.RERANKER_API_URL and not settings.RERANKER_MODEL:
        logger.warning("RERANKER_API_URL is configured but RERANKER_MODEL is empty. The API may reject requests.")

    if errors:
        raise ValueError("Runtime configuration validation failed: " + " | ".join(errors))
