"""MCP server for WideHoly — cross-religion scripture tools for AI assistants.

Requires the ``mcp`` extra: ``pip install wideholy[mcp]``

Run::

    uvx --from "wideholy[mcp]" python -m wideholy.mcp_server
"""

from __future__ import annotations

from typing import Any

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("wideholy")

BASE_URL = "https://wideholy.com"


def _client() -> "WideHoly":  # type: ignore[name-defined]  # noqa: F821, UP037
    from wideholy.api import WideHoly

    return WideHoly(base_url=BASE_URL)


@mcp.tool()
def verse_of_the_day(religion: str = "bible", date: str | None = None) -> dict[str, Any]:
    """Get the verse of the day for a religion (bible, quran, torah, gita, sutra).

    Returns a curated verse, deterministic per religion per date.
    """
    with _client() as c:
        return c.verse_of_the_day(religion=religion, date=date)


@mcp.tool()
def random_verse(religion: str | None = None) -> dict[str, Any]:
    """Get a random verse. If no religion specified, picks randomly from all 5."""
    with _client() as c:
        return c.random_verse(religion=religion)


@mcp.tool()
def compare_verses(ref_a: str, ref_b: str) -> dict[str, Any]:
    """Compare two verses side-by-side. Use religion:ref format for cross-religion.

    Examples:
    - Same religion: ref_a="genesis.1.1", ref_b="john.1.1"
    - Cross-religion: ref_a="bible:genesis.1.1", ref_b="quran:al-fatihah.1"
    """
    with _client() as c:
        return c.compare(ref_a=ref_a, ref_b=ref_b)


@mcp.tool()
def search_suggest(query: str, religion: str | None = None) -> dict[str, Any]:
    """Search for topics, people, and glossary terms across religions."""
    with _client() as c:
        return c.search_suggest(query=query, religion=religion)


@mcp.tool()
def topic_verses(topic: str) -> dict[str, Any]:
    """Get verses for a topic (e.g. love, creation, prayer) across all religions."""
    with _client() as c:
        return c.topic_verses(topic=topic)


@mcp.tool()
def cross_religion_topic(topic: str) -> dict[str, Any]:
    """Get cross-religion comparison for a universal spiritual theme.

    Available topics: creation, prayer, love, afterlife, compassion, fasting,
    forgiveness, gratitude, justice, wisdom.
    """
    with _client() as c:
        return c.cross_religion(topic=topic)


@mcp.tool()
def religious_calendar(
    religion: str | None = None,
    year: int | None = None,
    month: int | None = None,
) -> dict[str, Any]:
    """Get religious calendar events for 2026, filterable by religion and month."""
    with _client() as c:
        return c.calendar(religion=religion, year=year, month=month)


@mcp.tool()
def verse_commentary(verse_ref: str) -> dict[str, Any]:
    """Get commentary for a verse. Supports Quran tafsir, Torah Rashi, and Gita commentaries."""
    with _client() as c:
        return c.commentary(verse_ref=verse_ref)


if __name__ == "__main__":
    mcp.run()
