from __future__ import annotations

from acp.helpers import SessionUpdate
from acp.schema import (
    ContentToolCallContent,
    TextContentBlock,
    ToolCallLocation,
    ToolCallProgress,
    ToolCallStart,
)

from vibe import VIBE_ROOT
from vibe.acp.tools.session_update import (
    ToolCallSessionUpdateProtocol,
    ToolResultSessionUpdateProtocol,
    failed_tool_result,
    fallback_tool_call,
    resolve_kind,
)
from vibe.core.tools.builtins.webfetch import (
    WebFetch as CoreWebFetchTool,
    WebFetchArgs,
    WebFetchResult,
)
from vibe.core.types import ToolCallEvent, ToolResultEvent


class WebFetch(
    CoreWebFetchTool, ToolCallSessionUpdateProtocol, ToolResultSessionUpdateProtocol
):
    prompt_path = VIBE_ROOT / "core" / "tools" / "builtins" / "prompts" / "webfetch.md"

    @classmethod
    def tool_call_session_update(cls, event: ToolCallEvent) -> SessionUpdate | None:
        if not isinstance(event.args, WebFetchArgs):
            return fallback_tool_call(event, "web_fetch")

        url = cls._normalize_url(event.args.url)

        return ToolCallStart(
            session_update="tool_call",
            title=cls.get_call_display(event).summary,
            tool_call_id=event.tool_call_id,
            kind=resolve_kind(event.tool_name),
            raw_input=event.args.model_dump_json(),
            locations=[ToolCallLocation(path=url, field_meta={"type": "url"})],
            field_meta={"tool_name": event.tool_name},
        )

    @classmethod
    def tool_result_session_update(cls, event: ToolResultEvent) -> SessionUpdate | None:
        if failure := failed_tool_result(event, WebFetchResult):
            return failure

        result = event.result
        assert isinstance(result, WebFetchResult)

        return ToolCallProgress(
            session_update="tool_call_update",
            tool_call_id=event.tool_call_id,
            status="completed",
            content=[
                ContentToolCallContent(
                    type="content",
                    content=TextContentBlock(
                        type="text", text=cls.get_result_display(event).message
                    ),
                )
            ],
            kind=resolve_kind(event.tool_name),
            raw_output=result.model_dump_json(),
            locations=[
                ToolCallLocation(
                    path=result.url,
                    field_meta={
                        "type": "url",
                        "char_count": len(result.content),
                        "truncated": result.was_truncated,
                    },
                )
            ],
            field_meta={"tool_name": event.tool_name},
        )
