"""Resolution output generator — three deliverables on session resolve."""
import logging
from typing import Any
from uuid import UUID

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from app.models.ai_session import AISession
from app.models.session_resolution_output import SessionResolutionOutput
from app.services.assistant_chat_service import _call_ai

logger = logging.getLogger(__name__)

RESOLUTION_MODEL = "claude-sonnet-4-6"


class ResolutionOutputGenerator:
    def __init__(self, db: AsyncSession):
        self.db = db

    async def generate_all(self, session_id: UUID) -> list[SessionResolutionOutput]:
        result = await self.db.execute(
            select(AISession).where(AISession.id == session_id)
        )
        session = result.scalar_one_or_none()
        if not session:
            raise ValueError(f"Session {session_id} not found")

        context = self._build_session_context(session)

        outputs = []
        for output_type, prompt in [
            ("psa_ticket_notes", self._psa_notes_prompt(context)),
            ("knowledge_base", self._kb_article_prompt(context)),
            ("client_summary", self._client_summary_prompt(context)),
        ]:
            content, _, _ = await _call_ai(
                system_base="You are a technical documentation assistant for MSP teams.",
                rag_context="",
                history=[],
                new_message=prompt,
                max_tokens=2048,
            )

            output = SessionResolutionOutput(
                session_id=session_id,
                account_id=session.account_id,
                output_type=output_type,
                generated_content=content,
                status="draft",
                generated_by_model=RESOLUTION_MODEL,
            )
            self.db.add(output)
            outputs.append(output)

        await self.db.flush()
        return outputs

    async def edit_output(self, output_id: UUID, edited_content: str) -> SessionResolutionOutput:
        result = await self.db.execute(
            select(SessionResolutionOutput).where(SessionResolutionOutput.id == output_id)
        )
        output = result.scalar_one_or_none()
        if not output:
            raise ValueError(f"Output {output_id} not found")
        output.edited_content = edited_content
        await self.db.flush()
        return output

    async def push_output(self, output_id: UUID, destination: str) -> SessionResolutionOutput:
        result = await self.db.execute(
            select(SessionResolutionOutput).where(SessionResolutionOutput.id == output_id)
        )
        output = result.scalar_one_or_none()
        if not output:
            raise ValueError(f"Output {output_id} not found")

        from datetime import datetime, timezone
        output.status = "pushed"
        output.pushed_to = destination
        output.pushed_at = datetime.now(timezone.utc)
        await self.db.flush()
        return output

    def _build_session_context(self, session: AISession) -> str:
        intake = session.intake_content or {}
        intake_text = intake.get("text", "") or str(intake)
        parts = [
            f"Problem: {session.problem_summary or 'Unknown'}",
            f"Domain: {session.problem_domain or 'Unknown'}",
            f"Original intake: {intake_text[:300]}",
            f"Resolution: {session.resolution_summary or 'Not specified'}",
        ]

        steps = sorted(session.steps or [], key=lambda s: s.step_order)
        diagnostic = []
        follow_ups: list[str] = []
        for step in steps:
            content = step.content or {}
            step_type = content.get("type", "")
            if step_type == "resolution_suggestion":
                recs = content.get("follow_up_recommendations", [])
                if isinstance(recs, list):
                    follow_ups.extend(recs)
                continue
            description = content.get("text", "").strip()
            if not description:
                continue
            response = None
            if step.was_skipped:
                response = "skipped"
            elif step.selected_option and step.options_presented:
                for opt in step.options_presented:
                    if opt.get("value") == step.selected_option:
                        response = opt.get("label", step.selected_option)
                        break
                else:
                    response = step.selected_option
            elif step.selected_option:
                response = step.selected_option
            elif step.free_text_input:
                response = step.free_text_input
            entry = f"  {step.step_order + 1}. {description}"
            if response and response != "skipped":
                entry += f" — {response}"
            diagnostic.append(entry)

        if diagnostic:
            parts.append("\nDiagnostic steps:")
            parts.extend(diagnostic)
        if follow_ups:
            parts.append("\nRecommended follow-up:")
            parts.extend(f"  - {r}" for r in follow_ups)

        return "\n".join(parts)

    def _psa_notes_prompt(self, context: str) -> str:
        return (
            f"Generate professional PSA ticket notes for this resolved troubleshooting session.\n"
            f"Format as structured markdown with: Problem, Diagnostic Steps, Resolution, Recommendations.\n\n{context}"
        )

    def _kb_article_prompt(self, context: str) -> str:
        return (
            f"Generate a knowledge base article draft from this resolved session.\n"
            f"Include: Symptoms, Root Cause, Resolution Steps, Things to Rule Out First.\n\n{context}"
        )

    def _client_summary_prompt(self, context: str) -> str:
        return (
            f"Generate a non-technical summary for the end user/client.\n"
            f"Explain what was wrong and what was done to fix it in plain language.\n"
            f"No jargon. 2-3 paragraphs max.\n\n{context}"
        )
