"""content_publish tool — publish a file for inline viewing in the chat client.

The file must already exist in the session's file directory.
Publishing only registers metadata; the file itself is NOT copied.
The user sees the file via the existing /sessions/{id}/files/{name} endpoint,
so edits made by the agent are immediately visible.
"""

from pathlib import Path

from navi.content_store import publish
from navi.session_files import ensure_session_dir, session_dir

from .base import Tool, ToolResult, current_session_id


class ContentPublishTool(Tool):
    name = "content_publish"
    description = (
        "Publish a file for inline viewing in the chat client. "
        "Use this when you generate or produce content the user will want to see interactively "
        "(3D models, HTML pages, SVG graphics, images, videos, PDFs, etc.).\n\n"
        "IMPORTANT — the file MUST already be inside the session directory. "
        "Before publishing, write or move the file into the session folder. "
        "To find the session directory path, use `filesystem info <path>` or write directly to it. "
        "If a file with the same name already exists in the session directory, choose a different name "
        "or check the directory contents first with `filesystem list <session_dir>`.\n\n"
        "Best practices:\n"
        "- Always create files in the session directory (uploads/sessions/{session_id}/)\n"
        "- Use descriptive filenames (e.g., 'sales_chart.svg' not 'file.svg')\n"
        "- After publishing, you can edit the file directly and the user will see changes immediately\n"
        "- For images, use PNG or JPEG; for interactive content, use HTML or SVG"
    )
    parameters = {
        "type": "object",
        "properties": {
            "filename": {
                "type": "string",
                "description": (
                    "Name of the file to publish. "
                    "The file must already exist in the session directory. "
                    "Example: 'chart.svg' or 'report.html'."
                ),
            },
            "title": {
                "type": "string",
                "description": "Human-readable title shown on the content card",
            },
            "content_type": {
                "type": "string",
                "enum": ["stl", "html", "svg", "pdf", "image", "video", "unknown"],
                "description": "Content type for viewer selection. Auto-detected from extension if omitted.",
            },
        },
        "required": ["filename"],
    }

    async def execute(self, params: dict) -> ToolResult:
        session_id = current_session_id.get()
        if not session_id:
            return ToolResult(
                success=False,
                output="No active session context.",
                error="no_session",
            )

        filename = Path(params["filename"]).name  # strip any path components
        sess_dir = session_dir(session_id)
        src = sess_dir / filename

        if not src.exists():
            return ToolResult(
                success=False,
                output=(
                    f"File '{filename}' not found in the session directory: {sess_dir}\n"
                    f"Make sure the file was written or moved there before publishing. "
                    f"You can check the directory contents with `filesystem list {sess_dir}`."
                ),
                error="not_found",
            )
        if not src.is_file():
            return ToolResult(
                success=False,
                output=f"Path is not a file: {src}",
                error="not_a_file",
            )

        try:
            info = await publish(
                session_id=session_id,
                filename=filename,
                title=params.get("title"),
                content_type=params.get("content_type"),
            )
        except FileNotFoundError as e:
            return ToolResult(success=False, output=str(e), error="not_found")
        except IsADirectoryError as e:
            return ToolResult(success=False, output=str(e), error="is_directory")
        except Exception as e:
            return ToolResult(success=False, output=f"Publish failed: {e}", error="publish_failed")

        return ToolResult(
            success=True,
            output=(
                f"Published: {info['title']} ({info['content_type']})\n"
                f"URL: {info['url']}\n"
                f"ID: {info['id']}\n"
                f"If you need to edit this file later, edit it at: {src}"
            ),
            metadata=info,
        )
