"""
Query routes for the sim-search API.

This module defines the routes for query processing and classification.
"""

from typing import Any

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session

from app.api.dependencies import get_current_active_user
from app.db.models import User
from app.db.session import get_db
from app.schemas.query import QueryProcess, QueryClassify, ProcessedQuery
from app.services.query_service import QueryService

router = APIRouter()
query_service = QueryService()


@router.post("/process", response_model=ProcessedQuery)
async def process_query(
    query_in: QueryProcess,
    current_user: User = Depends(get_current_active_user),
    db: Session = Depends(get_db),
) -> Any:
    """
    Process a query to enhance and structure it.
    
    Args:
        query_in: Query to process
        current_user: Current authenticated user
        db: Database session
        
    Returns:
        Processed query with structured information
    """
    try:
        processed_query = await query_service.process_query(query_in.query)
        return processed_query
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Error processing query: {str(e)}",
        )


@router.post("/classify", response_model=ProcessedQuery)
async def classify_query(
    query_in: QueryClassify,
    current_user: User = Depends(get_current_active_user),
    db: Session = Depends(get_db),
) -> Any:
    """
    Classify a query by type and intent.
    
    Args:
        query_in: Query to classify
        current_user: Current authenticated user
        db: Database session
        
    Returns:
        Classified query with type and intent information
    """
    try:
        classified_query = await query_service.classify_query(query_in.query)
        return classified_query
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Error classifying query: {str(e)}",
        )
