"""Model Factory for creating language model instances.

Separates model creation logic from configuration.
"""

import dspy

from config import Config


class ModelFactory:
    """Factory class for creating language model instances based on configuration."""

    @staticmethod
    def create_dspy_model(agent_type: str, agent_name: str = None) -> dspy.LM:
        """Create a dspy.LM object for a specific agent with conditional parameters.

        Only includes api_base and api_key if they are configured.

        Args:
            agent_type (str): 'orchestrator' or 'expert'
            agent_name (str): For experts, specific agent name like 'weather', 'games'

        Returns:
            dspy.LM: Configured language model object

        """
        config = Config.Model.get_agent_config(agent_type, agent_name)

        # Build dspy.LM parameters conditionally
        lm_params = {"model": f"{config['provider']}/{config['model_name']}"}

        # Only add api_base if it's configured (not None)
        if config.get("api_base"):
            lm_params["api_base"] = config["api_base"]

        # Only add api_key if it's configured (not None)
        if config.get("api_key"):
            lm_params["api_key"] = config["api_key"]

        return dspy.LM(**lm_params)

    @staticmethod
    def create_orchestrator_model() -> dspy.LM:
        """Create orchestrator model."""
        return ModelFactory.create_dspy_model("orchestrator")

    @staticmethod
    def create_weather_model() -> dspy.LM:
        """Create weather expert model."""
        return ModelFactory.create_dspy_model("expert", "ingest")
