from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from services.rag_service import (
    rag_suggest_solution,
    rag_ask,
    rag_analyze_pattern,
    rag_classify_with_context,
)

router = APIRouter(tags=["rag"])


class AskRequest(BaseModel):
    question: str
    project_id: int | None = None


class PatternRequest(BaseModel):
    description: str
    n_results: int = 10


class ClassifyRequest(BaseModel):
    description: str
    detail_notes: str = ""


@router.post("/rag/suggest-solution/{issue_id}")
async def suggest_solution(issue_id: int):
    try:
        return await rag_suggest_solution(issue_id)
    except Exception as e:
        raise HTTPException(status_code=500, detail="AI 서비스 처리 중 오류가 발생했습니다")


@router.post("/rag/ask")
async def ask_question(req: AskRequest):
    try:
        return await rag_ask(req.question, req.project_id)
    except Exception as e:
        raise HTTPException(status_code=500, detail="AI 서비스 처리 중 오류가 발생했습니다")


@router.post("/rag/pattern")
async def analyze_pattern(req: PatternRequest):
    try:
        return await rag_analyze_pattern(req.description, req.n_results)
    except Exception as e:
        raise HTTPException(status_code=500, detail="AI 서비스 처리 중 오류가 발생했습니다")


@router.post("/rag/classify")
async def classify_with_rag(req: ClassifyRequest):
    try:
        return await rag_classify_with_context(req.description, req.detail_notes)
    except Exception as e:
        raise HTTPException(status_code=500, detail="AI 서비스 처리 중 오류가 발생했습니다")
