"""BibDoiTool — AXMTool for resolving DOIs to BibTeX."""

from __future__ import annotations

from typing import Any

from axm.tools.base import AXMTool, ToolResult

from axm_bib.pipeline import BibPipeline

__all__ = ["BibDoiTool"]


class BibDoiTool(AXMTool):
    """Resolve a DOI to a clean BibTeX entry.

    Registered as 'bib_doi' via axm.tools entry point.
    """

    @property
    def name(self) -> str:
        """Return the tool name used for entry-point registration."""
        return "bib_doi"

    def execute(self, *, doi: str = "", **kwargs: Any) -> ToolResult:
        """Resolve a DOI to BibTeX.

        Args:
            doi: Digital Object Identifier.

        Returns:
            ToolResult with data={"bibtex": "...", "key": "...", "doi": "..."}.
        """
        if not doi.strip():
            return ToolResult(success=False, error="DOI is required")

        try:
            pipeline = BibPipeline()
            entry = pipeline.resolve_bibtex(doi)
            return ToolResult(
                success=True,
                data={
                    "bibtex": entry.raw,
                    "key": entry.key,
                    "doi": entry.doi,
                    "entry_type": entry.entry_type,
                },
            )
        except Exception as exc:
            return ToolResult(success=False, error=str(exc))
