"""Factory helpers for creating analyzer instances."""

from __future__ import annotations

from typing import Any, Optional

from .analyzer import SlowQueryAnalyzer
from .mongodb_analyzer import MongoDBProfilerIntegration
from .mongodb_config import MongoDBThresholdConfig
from .postgresql.pgtools_analyzer import PostgreSQLAnalyzer


class AnalyzerFactory:
    """Factory for analyzer creation across supported database sources."""

    @staticmethod
    def create(
        database_type: str,
        *,
        source: str = "log",
        connection_string: Optional[str] = None,
        thresholds: Optional[MongoDBThresholdConfig] = None,
        ai_adapter: Optional[Any] = None,
        use_ai: bool = False,
    ) -> Any:
        normalized_database = database_type.strip().lower()
        normalized_source = source.strip().lower()

        if normalized_database in {"postgres", "postgresql"}:
            if normalized_source == "log":
                return SlowQueryAnalyzer()
            if normalized_source == "live":
                if not connection_string:
                    raise ValueError(
                        "connection_string is required for live PostgreSQL analyzer"
                    )
                return PostgreSQLAnalyzer(
                    connection_string=connection_string,
                    ai_adapter=ai_adapter,
                    use_ai=use_ai,
                )

        if normalized_database in {"mongo", "mongodb"} and normalized_source == "live":
            if not connection_string:
                raise ValueError(
                    "connection_string is required for live MongoDB analyzer"
                )
            return MongoDBProfilerIntegration(
                connection_string=connection_string,
                thresholds=thresholds or MongoDBThresholdConfig(),
                ai_adapter=ai_adapter,
                use_ai=use_ai,
            )

        raise ValueError(
            f"Unsupported analyzer combination: database_type={database_type!r}, "
            f"source={source!r}"
        )

    @staticmethod
    def create_postgres_log_analyzer() -> SlowQueryAnalyzer:
        """Create analyzer for PostgreSQL log inputs."""
        return SlowQueryAnalyzer()

    @staticmethod
    def create_postgres_live_analyzer(
        connection_string: str,
        *,
        ai_adapter: Optional[Any] = None,
        use_ai: bool = False,
    ) -> PostgreSQLAnalyzer:
        """Create analyzer for live PostgreSQL connections."""
        return PostgreSQLAnalyzer(
            connection_string=connection_string,
            ai_adapter=ai_adapter,
            use_ai=use_ai,
        )

    @staticmethod
    def create_mongodb_live_analyzer(
        connection_string: str,
        *,
        thresholds: Optional[MongoDBThresholdConfig] = None,
        ai_adapter: Optional[Any] = None,
        use_ai: bool = False,
    ) -> MongoDBProfilerIntegration:
        """Create analyzer for live MongoDB connections."""
        return MongoDBProfilerIntegration(
            connection_string=connection_string,
            thresholds=thresholds or MongoDBThresholdConfig(),
            ai_adapter=ai_adapter,
            use_ai=use_ai,
        )
