"""MCP tools for exploit intelligence."""

from typing import Any

from ..decorator.api import handle_api_errors
from ..server.server import get_client, mcp
from ..utils.serialize import paginated_to_dict


@mcp.tool()
@handle_api_errors
async def get_exploit(identifier: str) -> dict[str, Any]:
    """Get an exploit by UUID or identifier.

    Use for: exploit details, linked vulnerabilities, POC context.

    Args:
        identifier: Exploit UUID or identifier.

    Returns:
        Exploit record with description, links, related vulnerabilities.
    """
    return await get_client().exploits.get(identifier)


@mcp.tool()
@handle_api_errors
async def list_exploits(
    filter: str = "",
    offset: int = 0,
    limit: int = 10,
    sort: str = "created_at",
    order: str = "desc",
) -> dict[str, Any]:
    """List or search exploits with optional filters and pagination.

    Use for: discovering exploits, mapping to CVEs, trend analysis.

    Args:
        filter: Optional filter.
        offset: Pagination offset. Default 0.
        limit: Max items per page. Default 10.
        sort: Field to sort by. Default created_at.
        order: asc or desc.

    Returns:
        Paginated result with items, total, offset, limit, has_more.
    """
    resp = await get_client().exploits.list(
        filter=filter or None,
        offset=offset,
        limit=limit,
        sort=sort,
        order=order,
    )
    return paginated_to_dict(resp)
