"""Serialization helpers for the console LLM configuration UI."""

from __future__ import annotations

import os
from typing import Any

from django.db.models import Prefetch

from api.models import (
    BrowserLLMPolicy,
    BrowserLLMTier,
    BrowserTierEndpoint,
    BrowserModelEndpoint,
    EmbeddingsLLMTier,
    EmbeddingsTierEndpoint,
    EmbeddingsModelEndpoint,
    FileHandlerLLMTier,
    FileHandlerTierEndpoint,
    FileHandlerModelEndpoint,
    ImageGenerationLLMTier,
    ImageGenerationTierEndpoint,
    ImageGenerationModelEndpoint,
    IntelligenceTier,
    LLMProvider,
    LLMRoutingProfile,
    PersistentLLMTier,
    PersistentTierEndpoint,
    PersistentModelEndpoint,
    PersistentTokenRange,
    ProfileBrowserTier,
    ProfileBrowserTierEndpoint,
    ProfileEmbeddingsTier,
    ProfileEmbeddingsTierEndpoint,
    ProfilePersistentTier,
    ProfilePersistentTierEndpoint,
    ProfileTokenRange,
    VideoGenerationLLMTier,
    VideoGenerationTierEndpoint,
    VideoGenerationModelEndpoint,
)
from console.llm_tier_usage import (
    build_browser_endpoint_tier_usage_map,
    build_persistent_endpoint_tier_usage_map,
)


def _provider_key_status(provider: LLMProvider) -> str:
    has_admin_key = bool(provider.api_key_encrypted)
    has_env = bool(provider.env_var_name and os.getenv(provider.env_var_name))
    if not (has_admin_key or has_env):
        return "Missing key"
    if has_admin_key:
        return "Admin key"
    return "Env var"


def _serialize_endpoint_common(endpoint, *, label: str) -> dict[str, Any]:
    return {
        "id": str(endpoint.id),
        "label": label,
        "enabled": bool(endpoint.enabled),
        "low_latency": bool(getattr(endpoint, "low_latency", False)),
    }


def _serialize_persistent_endpoint(
    endpoint: PersistentModelEndpoint,
    tier_usage_by_endpoint: dict[Any, list[dict[str, Any]]],
) -> dict[str, Any]:
    label = f"{endpoint.provider.display_name} · {endpoint.litellm_model}"
    data = _serialize_endpoint_common(endpoint, label=label)
    data.update(
        {
            "key": endpoint.key,
            "model": endpoint.litellm_model,
            "temperature_override": endpoint.temperature_override,
            "supports_temperature": endpoint.supports_temperature,
            "supports_tool_choice": endpoint.supports_tool_choice,
            "use_parallel_tool_calls": endpoint.use_parallel_tool_calls,
            "allow_implied_send": endpoint.allow_implied_send,
            "supports_vision": endpoint.supports_vision,
            "supports_reasoning": endpoint.supports_reasoning,
            "reasoning_effort": endpoint.reasoning_effort,
            "api_base": endpoint.api_base,
            "openrouter_preset": endpoint.openrouter_preset,
            "max_input_tokens": endpoint.max_input_tokens,
            "provider_id": str(endpoint.provider_id),
            "type": "persistent",
            "tier_usage": tier_usage_by_endpoint.get(endpoint.id, []),
        }
    )
    return data


def _serialize_browser_endpoint(
    endpoint: BrowserModelEndpoint,
    tier_usage_by_endpoint: dict[Any, list[dict[str, Any]]],
) -> dict[str, Any]:
    label = f"{endpoint.provider.display_name} · {endpoint.browser_model}"
    data = _serialize_endpoint_common(endpoint, label=label)
    data.update(
        {
            "key": endpoint.key,
            "model": endpoint.browser_model,
            "browser_base_url": endpoint.browser_base_url,
            "supports_temperature": endpoint.supports_temperature,
            "supports_vision": endpoint.supports_vision,
            "max_output_tokens": endpoint.max_output_tokens,
            "provider_id": str(endpoint.provider_id),
            "type": "browser",
            "tier_usage": tier_usage_by_endpoint.get(endpoint.id, []),
        }
    )
    return data


def _serialize_embedding_endpoint(endpoint: EmbeddingsModelEndpoint) -> dict[str, Any]:
    return _serialize_aux_endpoint(endpoint, endpoint_type="embedding")


def _serialize_aux_endpoint(
    endpoint: EmbeddingsModelEndpoint | FileHandlerModelEndpoint | ImageGenerationModelEndpoint | VideoGenerationModelEndpoint,
    *,
    endpoint_type: str,
) -> dict[str, Any]:
    label = f"{endpoint.provider.display_name if endpoint.provider else 'Unlinked'} · {endpoint.litellm_model}"
    data = _serialize_endpoint_common(endpoint, label=label)
    data.update(
        {
            "key": endpoint.key,
            "model": endpoint.litellm_model,
            "api_base": endpoint.api_base,
            "provider_id": str(endpoint.provider_id) if endpoint.provider_id else None,
            "type": endpoint_type,
        }
    )
    if endpoint_type == "file_handler":
        data["supports_vision"] = bool(getattr(endpoint, "supports_vision", False))
    if endpoint_type == "image_generation":
        data["supports_image_to_image"] = bool(getattr(endpoint, "supports_image_to_image", False))
    if endpoint_type == "video_generation":
        data["supports_image_to_video"] = bool(getattr(endpoint, "supports_image_to_video", False))
    return data


def _serialize_file_handler_endpoint(endpoint: FileHandlerModelEndpoint) -> dict[str, Any]:
    return _serialize_aux_endpoint(endpoint, endpoint_type="file_handler")


def _serialize_image_generation_endpoint(endpoint: ImageGenerationModelEndpoint) -> dict[str, Any]:
    return _serialize_aux_endpoint(endpoint, endpoint_type="image_generation")


def _serialize_video_generation_endpoint(endpoint: VideoGenerationModelEndpoint) -> dict[str, Any]:
    return _serialize_aux_endpoint(endpoint, endpoint_type="video_generation")


def _serialize_weighted_endpoint_reference(endpoint, tier_endpoint) -> dict[str, Any]:
    provider_name = endpoint.provider.display_name if endpoint.provider else "Unlinked"
    return {
        "id": str(tier_endpoint.id),
        "endpoint_id": str(endpoint.id),
        "label": f"{provider_name} · {endpoint.litellm_model}",
        "weight": float(tier_endpoint.weight),
        "endpoint_key": endpoint.key,
    }


def _serialize_weighted_tier_payload(tiers) -> list[dict[str, Any]]:
    payload: list[dict[str, Any]] = []
    for tier in tiers:
        tier_endpoints = [
            _serialize_weighted_endpoint_reference(tier_endpoint.endpoint, tier_endpoint)
            for tier_endpoint in tier.tier_endpoints.all()
        ]
        payload.append(
            {
                "id": str(tier.id),
                "order": tier.order,
                "description": tier.description,
                "use_case": getattr(tier, "use_case", None),
                "endpoints": tier_endpoints,
            }
        )
    return payload


def build_llm_overview() -> dict[str, Any]:
    providers = list(
        LLMProvider.objects.all()
        .order_by("display_name")
        .prefetch_related(
            Prefetch("persistent_endpoints", queryset=PersistentModelEndpoint.objects.select_related("provider")),
            Prefetch("browser_endpoints", queryset=BrowserModelEndpoint.objects.select_related("provider")),
            Prefetch("embedding_endpoints", queryset=EmbeddingsModelEndpoint.objects.select_related("provider")),
            Prefetch("file_handler_endpoints", queryset=FileHandlerModelEndpoint.objects.select_related("provider")),
            Prefetch("image_generation_endpoints", queryset=ImageGenerationModelEndpoint.objects.select_related("provider")),
            Prefetch("video_generation_endpoints", queryset=VideoGenerationModelEndpoint.objects.select_related("provider")),
        )
    )
    persistent_tier_usage_by_endpoint = build_persistent_endpoint_tier_usage_map(
        endpoint.id
        for provider in providers
        for endpoint in provider.persistent_endpoints.all()
    )
    browser_tier_usage_by_endpoint = build_browser_endpoint_tier_usage_map(
        endpoint.id
        for provider in providers
        for endpoint in provider.browser_endpoints.all()
    )

    provider_payload: list[dict[str, Any]] = []
    persistent_choices: list[dict[str, Any]] = []
    browser_choices: list[dict[str, Any]] = []
    embedding_choices: list[dict[str, Any]] = []
    file_handler_choices: list[dict[str, Any]] = []
    image_generation_choices: list[dict[str, Any]] = []
    video_generation_choices: list[dict[str, Any]] = []

    for provider in providers:
        persistent_endpoints = [
            _serialize_persistent_endpoint(endpoint, persistent_tier_usage_by_endpoint)
            for endpoint in provider.persistent_endpoints.all()
        ]
        browser_endpoints = [
            _serialize_browser_endpoint(endpoint, browser_tier_usage_by_endpoint)
            for endpoint in provider.browser_endpoints.all()
        ]
        embedding_endpoints = [
            _serialize_embedding_endpoint(endpoint)
            for endpoint in provider.embedding_endpoints.all()
        ]
        file_handler_endpoints = [
            _serialize_file_handler_endpoint(endpoint)
            for endpoint in provider.file_handler_endpoints.all()
        ]
        image_generation_endpoints = [
            _serialize_image_generation_endpoint(endpoint)
            for endpoint in provider.image_generation_endpoints.all()
        ]
        video_generation_endpoints = [
            _serialize_video_generation_endpoint(endpoint)
            for endpoint in provider.video_generation_endpoints.all()
        ]

        persistent_choices.extend(persistent_endpoints)
        browser_choices.extend(browser_endpoints)
        embedding_choices.extend(embedding_endpoints)
        file_handler_choices.extend(file_handler_endpoints)
        image_generation_choices.extend(image_generation_endpoints)
        video_generation_choices.extend(video_generation_endpoints)

        provider_payload.append(
            {
                "id": str(provider.id),
                "name": provider.display_name,
                "key": provider.key,
                "enabled": bool(provider.enabled),
                "env_var": provider.env_var_name,
                "browser_backend": provider.browser_backend,
                "supports_safety_identifier": provider.supports_safety_identifier,
                "vertex_project": provider.vertex_project,
                "vertex_location": provider.vertex_location,
                "status": _provider_key_status(provider),
                "endpoints": (
                    persistent_endpoints
                    + browser_endpoints
                    + embedding_endpoints
                    + file_handler_endpoints
                    + image_generation_endpoints
                    + video_generation_endpoints
                ),
            }
        )

    persistent_ranges = (
        PersistentTokenRange.objects.order_by("min_tokens")
        .prefetch_related(
            Prefetch(
                "tiers",
                queryset=PersistentLLMTier.objects.select_related("intelligence_tier").order_by(
                    "intelligence_tier__rank",
                    "order",
                ).prefetch_related(
                    Prefetch(
                        "tier_endpoints",
                        queryset=PersistentTierEndpoint.objects.select_related("endpoint__provider").order_by("endpoint__litellm_model"),
                    )
                ),
            )
        )
    )

    persistent_payload: list[dict[str, Any]] = []
    for token_range in persistent_ranges:
        tiers_payload: list[dict[str, Any]] = []
        for tier in token_range.tiers.all():
            tier_endpoints = []
            for te in tier.tier_endpoints.all():
                endpoint = te.endpoint
                tier_endpoints.append(
                    {
                        "id": str(te.id),
                        "endpoint_id": str(endpoint.id),
                        "label": f"{endpoint.provider.display_name} · {endpoint.litellm_model}",
                        "weight": float(te.weight),
                        "endpoint_key": endpoint.key,
                        "reasoning_effort_override": te.reasoning_effort_override,
                        "supports_reasoning": endpoint.supports_reasoning,
                        "endpoint_reasoning_effort": endpoint.reasoning_effort,
                    }
                )
            tiers_payload.append(
                {
                    "id": str(tier.id),
                    "order": tier.order,
                    "description": tier.description,
                    "intelligence_tier": {
                        "key": tier.intelligence_tier.key,
                        "display_name": tier.intelligence_tier.display_name,
                        "rank": tier.intelligence_tier.rank,
                        "credit_multiplier": str(tier.intelligence_tier.credit_multiplier),
                    },
                    "endpoints": tier_endpoints,
                }
            )
        persistent_payload.append(
            {
                "id": str(token_range.id),
                "name": token_range.name,
                "min_tokens": token_range.min_tokens,
                "max_tokens": token_range.max_tokens,
                "tiers": tiers_payload,
            }
        )

    policy = (
        BrowserLLMPolicy.objects.filter(is_active=True)
        .prefetch_related(
            Prefetch(
                "tiers",
                queryset=BrowserLLMTier.objects.select_related("intelligence_tier").order_by(
                    "intelligence_tier__rank",
                    "order",
                ).prefetch_related(
            Prefetch(
                "tier_endpoints",
                queryset=BrowserTierEndpoint.objects.select_related(
                    "endpoint__provider",
                    "extraction_endpoint__provider",
                ).order_by("endpoint__browser_model"),
            )
        ),
            )
        )
        .first()
    )

    browser_payload: dict[str, Any] | None = None
    if policy:
        tiers_payload: list[dict[str, Any]] = []
        for tier in policy.tiers.all():
            tier_endpoints = []
            for te in tier.tier_endpoints.all():
                endpoint = te.endpoint
                extraction = te.extraction_endpoint
                tier_endpoints.append(
                    {
                        "id": str(te.id),
                        "endpoint_id": str(endpoint.id),
                        "label": f"{endpoint.provider.display_name} · {endpoint.browser_model}",
                        "weight": float(te.weight),
                        "endpoint_key": endpoint.key,
                        "extraction_endpoint_id": str(extraction.id) if extraction else None,
                        "extraction_endpoint_key": extraction.key if extraction else None,
                        "extraction_label": (
                            f"{extraction.provider.display_name} · {extraction.browser_model}"
                            if extraction
                            else None
                        ),
                    }
                )
            tiers_payload.append(
                {
                    "id": str(tier.id),
                    "order": tier.order,
                    "description": tier.description,
                    "intelligence_tier": {
                        "key": tier.intelligence_tier.key,
                        "display_name": tier.intelligence_tier.display_name,
                        "rank": tier.intelligence_tier.rank,
                        "credit_multiplier": str(tier.intelligence_tier.credit_multiplier),
                    },
                    "endpoints": tier_endpoints,
                }
            )
        browser_payload = {
            "id": str(policy.id),
            "name": policy.name,
            "tiers": tiers_payload,
        }

    embedding_tiers = EmbeddingsLLMTier.objects.prefetch_related(
        Prefetch(
            "tier_endpoints",
            queryset=EmbeddingsTierEndpoint.objects.select_related("endpoint__provider").order_by("-weight"),
        )
    ).order_by("order")
    embedding_payload = _serialize_weighted_tier_payload(embedding_tiers)

    file_handler_tiers = FileHandlerLLMTier.objects.prefetch_related(
        Prefetch(
            "tier_endpoints",
            queryset=FileHandlerTierEndpoint.objects.select_related("endpoint__provider").order_by("-weight"),
        )
    ).order_by("order")
    file_handler_payload = _serialize_weighted_tier_payload(file_handler_tiers)

    image_generation_tiers = ImageGenerationLLMTier.objects.prefetch_related(
        Prefetch(
            "tier_endpoints",
            queryset=ImageGenerationTierEndpoint.objects.select_related("endpoint__provider").order_by("-weight"),
        )
    ).order_by("use_case", "order")
    create_image_generation_payload = _serialize_weighted_tier_payload(
        image_generation_tiers.filter(use_case=ImageGenerationLLMTier.UseCase.CREATE_IMAGE)
    )
    avatar_image_generation_payload = _serialize_weighted_tier_payload(
        image_generation_tiers.filter(use_case=ImageGenerationLLMTier.UseCase.AVATAR)
    )

    video_generation_tiers = VideoGenerationLLMTier.objects.prefetch_related(
        Prefetch(
            "tier_endpoints",
            queryset=VideoGenerationTierEndpoint.objects.select_related("endpoint__provider").order_by("-weight"),
        )
    ).order_by("use_case", "order")
    create_video_generation_payload = _serialize_weighted_tier_payload(
        video_generation_tiers.filter(use_case=VideoGenerationLLMTier.UseCase.CREATE_VIDEO)
    )

    stats = {
        "active_providers": LLMProvider.objects.filter(enabled=True).count(),
        "persistent_endpoints": PersistentModelEndpoint.objects.filter(enabled=True).count(),
        "browser_endpoints": BrowserModelEndpoint.objects.filter(enabled=True).count(),
        "premium_persistent_tiers": PersistentLLMTier.objects.filter(intelligence_tier__key="premium").count(),
    }
    intelligence_tiers = [
        {
            "key": tier.key,
            "display_name": tier.display_name,
            "rank": tier.rank,
            "credit_multiplier": str(tier.credit_multiplier),
        }
        for tier in IntelligenceTier.objects.order_by("rank")
    ]

    return {
        "stats": stats,
        "intelligence_tiers": intelligence_tiers,
        "providers": provider_payload,
        "persistent": {"ranges": persistent_payload},
        "browser": browser_payload,
        "embeddings": {"tiers": embedding_payload},
        "file_handlers": {"tiers": file_handler_payload},
        "image_generations": {
            "create_image_tiers": create_image_generation_payload,
            "avatar_tiers": avatar_image_generation_payload,
        },
        "video_generations": {
            "create_video_tiers": create_video_generation_payload,
        },
        "choices": {
            "persistent_endpoints": persistent_choices,
            "browser_endpoints": browser_choices,
            "embedding_endpoints": embedding_choices,
            "file_handler_endpoints": file_handler_choices,
            "image_generation_endpoints": image_generation_choices,
            "video_generation_endpoints": video_generation_choices,
        },
    }


def serialize_routing_profile_list_item(profile: LLMRoutingProfile) -> dict[str, Any]:
    """Serialize a profile for list views (minimal details)."""
    return {
        "id": str(profile.id),
        "name": profile.name,
        "display_name": profile.display_name,
        "description": profile.description,
        "is_active": profile.is_active,
        "is_eval_snapshot": profile.is_eval_snapshot,
        "created_at": profile.created_at.isoformat() if profile.created_at else None,
        "updated_at": profile.updated_at.isoformat() if profile.updated_at else None,
        "cloned_from_id": str(profile.cloned_from_id) if profile.cloned_from_id else None,
        "eval_judge_endpoint_id": str(profile.eval_judge_endpoint_id) if profile.eval_judge_endpoint_id else None,
        "summarization_endpoint_id": (
            str(profile.summarization_endpoint_id) if profile.summarization_endpoint_id else None
        ),
        "agent_judge_endpoint_id": str(profile.agent_judge_endpoint_id) if profile.agent_judge_endpoint_id else None,
    }


def serialize_routing_profile_detail(profile: LLMRoutingProfile) -> dict[str, Any]:
    """
    Serialize a full routing profile with all nested config.
    Expects the profile to have prefetched related objects.
    """
    # Persistent config: token ranges -> tiers -> endpoints
    persistent_ranges: list[dict[str, Any]] = []
    for token_range in profile.persistent_token_ranges.all():
        tiers_payload: list[dict[str, Any]] = []
        for tier in token_range.tiers.all():
            tier_endpoints = []
            for te in tier.tier_endpoints.all():
                endpoint = te.endpoint
                tier_endpoints.append({
                    "id": str(te.id),
                    "endpoint_id": str(endpoint.id),
                    "label": f"{endpoint.provider.display_name} · {endpoint.litellm_model}",
                    "weight": float(te.weight),
                    "endpoint_key": endpoint.key,
                    "reasoning_effort_override": te.reasoning_effort_override,
                    "supports_reasoning": endpoint.supports_reasoning,
                    "endpoint_reasoning_effort": endpoint.reasoning_effort,
                })
            tiers_payload.append({
                "id": str(tier.id),
                "order": tier.order,
                "description": tier.description,
                "intelligence_tier": {
                    "key": tier.intelligence_tier.key,
                    "display_name": tier.intelligence_tier.display_name,
                    "rank": tier.intelligence_tier.rank,
                    "credit_multiplier": str(tier.intelligence_tier.credit_multiplier),
                },
                "endpoints": tier_endpoints,
            })
        persistent_ranges.append({
            "id": str(token_range.id),
            "name": token_range.name,
            "min_tokens": token_range.min_tokens,
            "max_tokens": token_range.max_tokens,
            "tiers": tiers_payload,
        })

    # Browser config: tiers -> endpoints
    browser_tiers: list[dict[str, Any]] = []
    for tier in profile.browser_tiers.all():
        tier_endpoints = []
        for te in tier.tier_endpoints.all():
            endpoint = te.endpoint
            extraction = te.extraction_endpoint
            tier_endpoints.append({
                "id": str(te.id),
                "endpoint_id": str(endpoint.id),
                "label": f"{endpoint.provider.display_name} · {endpoint.browser_model}",
                "weight": float(te.weight),
                "endpoint_key": endpoint.key,
                "extraction_endpoint_id": str(extraction.id) if extraction else None,
                "extraction_endpoint_key": extraction.key if extraction else None,
                "extraction_label": (
                    f"{extraction.provider.display_name} · {extraction.browser_model}"
                    if extraction
                    else None
                ),
            })
        browser_tiers.append({
            "id": str(tier.id),
            "order": tier.order,
            "description": tier.description,
            "intelligence_tier": {
                "key": tier.intelligence_tier.key,
                "display_name": tier.intelligence_tier.display_name,
                "rank": tier.intelligence_tier.rank,
                "credit_multiplier": str(tier.intelligence_tier.credit_multiplier),
            },
            "endpoints": tier_endpoints,
        })

    # Embeddings config: tiers -> endpoints
    embedding_tiers = _serialize_weighted_tier_payload(profile.embeddings_tiers.all())

    # Eval judge endpoint info
    eval_judge_endpoint = None
    if profile.eval_judge_endpoint:
        ep = profile.eval_judge_endpoint
        provider_name = ep.provider.display_name if ep.provider else "Unlinked"
        eval_judge_endpoint = {
            "endpoint_id": str(ep.id),
            "endpoint_key": ep.key,
            "label": f"{provider_name} · {ep.litellm_model}",
            "model": ep.litellm_model,
        }

    summarization_endpoint = None
    if profile.summarization_endpoint:
        ep = profile.summarization_endpoint
        provider_name = ep.provider.display_name if ep.provider else "Unlinked"
        summarization_endpoint = {
            "endpoint_id": str(ep.id),
            "endpoint_key": ep.key,
            "label": f"{provider_name} · {ep.litellm_model}",
            "model": ep.litellm_model,
        }

    agent_judge_endpoint = None
    if profile.agent_judge_endpoint:
        ep = profile.agent_judge_endpoint
        provider_name = ep.provider.display_name if ep.provider else "Unlinked"
        agent_judge_endpoint = {
            "endpoint_id": str(ep.id),
            "endpoint_key": ep.key,
            "label": f"{provider_name} · {ep.litellm_model}",
            "model": ep.litellm_model,
        }

    return {
        "id": str(profile.id),
        "name": profile.name,
        "display_name": profile.display_name,
        "description": profile.description,
        "is_active": profile.is_active,
        "is_eval_snapshot": profile.is_eval_snapshot,
        "created_at": profile.created_at.isoformat() if profile.created_at else None,
        "updated_at": profile.updated_at.isoformat() if profile.updated_at else None,
        "cloned_from_id": str(profile.cloned_from_id) if profile.cloned_from_id else None,
        "eval_judge_endpoint": eval_judge_endpoint,
        "summarization_endpoint": summarization_endpoint,
        "agent_judge_endpoint": agent_judge_endpoint,
        "persistent": {"ranges": persistent_ranges},
        "browser": {"tiers": browser_tiers},
        "embeddings": {"tiers": embedding_tiers},
    }


def build_routing_profiles_list() -> list[dict[str, Any]]:
    """Build a list of all routing profiles (minimal details).

    Excludes eval snapshots which are frozen copies created for eval runs.
    """
    profiles = LLMRoutingProfile.objects.filter(is_eval_snapshot=False).order_by("-is_active", "-updated_at")
    return [serialize_routing_profile_list_item(p) for p in profiles]


def get_routing_profile_with_prefetch(profile_id: str) -> LLMRoutingProfile:
    """
    Fetch a routing profile with all nested relations prefetched for serialization.
    """
    # Prefetch for persistent: token_ranges -> tiers -> tier_endpoints -> endpoint.provider
    persistent_tier_endpoint_prefetch = Prefetch(
        "tier_endpoints",
        queryset=ProfilePersistentTierEndpoint.objects.select_related("endpoint__provider").order_by("endpoint__litellm_model"),
    )
    persistent_tier_prefetch = Prefetch(
        "tiers",
        queryset=ProfilePersistentTier.objects.select_related("intelligence_tier").prefetch_related(
            persistent_tier_endpoint_prefetch
        ).order_by("intelligence_tier__rank", "order"),
    )
    persistent_range_prefetch = Prefetch(
        "persistent_token_ranges",
        queryset=ProfileTokenRange.objects.prefetch_related(persistent_tier_prefetch).order_by("min_tokens"),
    )

    # Prefetch for browser: browser_tiers -> tier_endpoints -> endpoint.provider
    browser_tier_endpoint_prefetch = Prefetch(
        "tier_endpoints",
        queryset=ProfileBrowserTierEndpoint.objects.select_related(
            "endpoint__provider",
            "extraction_endpoint__provider",
        ).order_by("endpoint__browser_model"),
    )
    browser_tier_prefetch = Prefetch(
        "browser_tiers",
        queryset=ProfileBrowserTier.objects.select_related("intelligence_tier").prefetch_related(
            browser_tier_endpoint_prefetch
        ).order_by("intelligence_tier__rank", "order"),
    )

    # Prefetch for embeddings: embeddings_tiers -> tier_endpoints -> endpoint.provider
    embedding_tier_endpoint_prefetch = Prefetch(
        "tier_endpoints",
        queryset=ProfileEmbeddingsTierEndpoint.objects.select_related("endpoint__provider").order_by("-weight"),
    )
    embedding_tier_prefetch = Prefetch(
        "embeddings_tiers",
        queryset=ProfileEmbeddingsTier.objects.prefetch_related(embedding_tier_endpoint_prefetch).order_by("order"),
    )

    return LLMRoutingProfile.objects.select_related(
        "eval_judge_endpoint__provider",
        "summarization_endpoint__provider",
        "agent_judge_endpoint__provider",
    ).prefetch_related(
        persistent_range_prefetch,
        browser_tier_prefetch,
        embedding_tier_prefetch,
    ).get(id=profile_id)


__all__ = [
    "build_llm_overview",
    "build_routing_profiles_list",
    "get_routing_profile_with_prefetch",
    "serialize_routing_profile_detail",
    "serialize_routing_profile_list_item",
]
