import json
from services.ollama_client import ollama_client
from services.utils import load_prompt, parse_json_response
from config import settings


CLASSIFY_PROMPT_PATH = "prompts/classify_issue.txt"
SUMMARIZE_PROMPT_PATH = "prompts/summarize_issue.txt"


async def classify_issue(description: str, detail_notes: str = "") -> dict:
    template = load_prompt(CLASSIFY_PROMPT_PATH)
    prompt = template.format(
        description=description or "",
        detail_notes=detail_notes or "",
    )
    raw = await ollama_client.generate_text(prompt)
    try:
        start = raw.find("{")
        end = raw.rfind("}") + 1
        if start >= 0 and end > start:
            return json.loads(raw[start:end])
    except json.JSONDecodeError:
        pass
    return {"raw_response": raw, "parse_error": True}


async def summarize_issue(
    description: str, detail_notes: str = "", solution: str = ""
) -> dict:
    template = load_prompt(SUMMARIZE_PROMPT_PATH)
    prompt = template.format(
        description=description or "",
        detail_notes=detail_notes or "",
        solution=solution or "",
    )
    raw = await ollama_client.generate_text(prompt)
    try:
        start = raw.find("{")
        end = raw.rfind("}") + 1
        if start >= 0 and end > start:
            return json.loads(raw[start:end])
    except json.JSONDecodeError:
        pass
    return {"summary": raw.strip()}


async def classify_and_summarize(
    description: str, detail_notes: str = ""
) -> dict:
    classification = await classify_issue(description, detail_notes)
    summary_result = await summarize_issue(description, detail_notes)
    return {
        "classification": classification,
        "summary": summary_result.get("summary", ""),
    }
