from app.services.kb_service import KBService
from app.services.intent_service import IntentService
from app.schemas.chat import ChatResponse


class TroubleshootingService:
    def __init__(self) -> None:
        self.kb = KBService()

    def process_message(self, message: str) -> ChatResponse:
        flows = self.kb.get_all_flows()
        intent_service = IntentService(flows)
        issue = intent_service.detect_issue(message)

        if not issue:
            return ChatResponse(
                reply=(
                    "I could not identify the exact issue. "
                    "Please describe whether it is related to Laptop, Desktop, Wi-Fi, "
                    "VPN, Printer, Outlook, Login, Shared Drive, or Network."
                ),
                options=[
                    "Laptop slow",
                    "Wi-Fi no internet",
                    "VPN not connecting",
                    "Outlook not opening",
                    "Printer offline",
                    "Shared drive inaccessible",
                    "Create ticket"
                ],
                ticket_suggestion=True
            )

        steps = issue.get("steps", [])
        formatted_reply = (
            f"I found a troubleshooting guide for: {issue.get('issue_title')}\n\n"
            "Please try these steps one by one."
        )

        return ChatResponse(
            issue_code=issue.get("code"),
            issue_title=issue.get("issue_title"),
            detected_category=issue.get("category"),
            reply=formatted_reply,
            steps=steps,
            options=["Resolved", "Not resolved", "Create ticket"],
            ticket_suggestion=issue.get("auto_ticket_on_failure", True)
        )