"""Route queries — all execute against local PostgreSQL.

Since we sync remote data into local PG, all queries run locally.
The router classifies the user's intent (SQL vs conversational).
"""

from __future__ import annotations

from local_llm.ollama_client import OllamaClient


class QueryRouter:
    """Classifies user questions and routes appropriately."""

    def __init__(self, ollama: OllamaClient) -> None:
        self._ollama = ollama

    async def classify(self, question: str) -> str:
        """Classify user question using local LLM.

        Returns: 'sql_query' | 'conversational' | 'clarification_needed'
        """
        prompt = f"""Classify this user message into one of these categories:

1. sql_query - The user wants to query or analyze data (e.g., "How many orders last month?", "Show me top customers", "What's the total revenue?")
2. conversational - The user is chatting, asking about capabilities, greeting, etc. (e.g., "Hello", "What can you do?", "Thanks!")
3. clarification_needed - The question is about data but too vague to create a SQL query (e.g., "Show me the data", "What about orders?")

User message: "{question}"

Respond with ONLY the category name (sql_query, conversational, or clarification_needed):"""

        response = await self._ollama.generate(
            prompt=prompt,
            temperature=0.1,
            max_tokens=20,
        )

        result = response.strip().lower().replace('"', "").replace("'", "")
        if "sql_query" in result:
            return "sql_query"
        elif "clarification" in result:
            return "clarification_needed"
        else:
            return "conversational"
