"""
Model Context Protocol (MCP) server for Utsuho.

This module exposes Utsuho's text conversion utilities as MCP tools.
"""

import sys

from .converters import (
    FullToHalfConverter,
    HalfToFullConverter,
    HiraganaToKatakanaConverter,
    KatakanaToHiraganaConverter,
    WidthConverterConfig,
)


# pylint: disable=R0913,R0917
def _build_width_converter_config(
    punctuation: bool = True,
    corner_brucket: bool = True,
    conjunction_mark: bool = True,
    length_mark: bool = True,
    space: bool = True,
    ascii_symbol: bool = True,
    ascii_digit: bool = True,
    ascii_alphabet: bool = True,
    wave_dash: bool = False,
) -> WidthConverterConfig:
    """
    Build a width conversion configuration for MCP tools.
    """
    return WidthConverterConfig(
        punctuation=punctuation,
        corner_brucket=corner_brucket,
        conjunction_mark=conjunction_mark,
        length_mark=length_mark,
        space=space,
        ascii_symbol=ascii_symbol,
        ascii_digit=ascii_digit,
        ascii_alphabet=ascii_alphabet,
        wave_dash=wave_dash,
    )


def _create_mcp():
    """
    Create and configure the Utsuho MCP server.
    """
    try:
        # pylint: disable=C0415
        from fastmcp import FastMCP
    except ImportError as exc:
        raise RuntimeError(
            'FastMCP is required to run the Utsuho MCP server. '
            'Install it with: pip install "Utsuho[mcp]"'
        ) from exc

    mcp = FastMCP("Utsuho")

    _hira_to_kata = HiraganaToKatakanaConverter()
    _kata_to_hira = KatakanaToHiraganaConverter()

    @mcp.tool
    # pylint: disable=R0913,R0917
    def half_to_full(
        text: str,
        punctuation: bool = True,
        corner_brucket: bool = True,
        conjunction_mark: bool = True,
        length_mark: bool = True,
        space: bool = True,
        ascii_symbol: bool = True,
        ascii_digit: bool = True,
        ascii_alphabet: bool = True,
    ) -> str:
        config = _build_width_converter_config(
            punctuation=punctuation,
            corner_brucket=corner_brucket,
            conjunction_mark=conjunction_mark,
            length_mark=length_mark,
            space=space,
            ascii_symbol=ascii_symbol,
            ascii_digit=ascii_digit,
            ascii_alphabet=ascii_alphabet,
        )
        return HalfToFullConverter(config).convert(text)

    @mcp.tool
    # pylint: disable=R0913,R0917
    def full_to_half(
        text: str,
        punctuation: bool = True,
        corner_brucket: bool = True,
        conjunction_mark: bool = True,
        length_mark: bool = True,
        space: bool = True,
        ascii_symbol: bool = True,
        ascii_digit: bool = True,
        ascii_alphabet: bool = True,
        wave_dash: bool = False,
    ) -> str:
        config = _build_width_converter_config(
            punctuation=punctuation,
            corner_brucket=corner_brucket,
            conjunction_mark=conjunction_mark,
            length_mark=length_mark,
            space=space,
            ascii_symbol=ascii_symbol,
            ascii_digit=ascii_digit,
            ascii_alphabet=ascii_alphabet,
            wave_dash=wave_dash,
        )
        return FullToHalfConverter(config).convert(text)

    @mcp.tool
    def hiragana_to_katakana(text: str) -> str:
        return _hira_to_kata.convert(text)

    @mcp.tool
    def katakana_to_hiragana(text: str) -> str:
        return _kata_to_hira.convert(text)

    return mcp


def main() -> None:
    """
    Run the Utsuho MCP server.
    """
    try:
        mcp = _create_mcp()
    except RuntimeError as exc:
        print(f"Error: {exc}", file=sys.stderr)
        raise SystemExit(1) from None

    mcp.run(
        transport="stdio",
        log_level="WARNING",
        show_banner=False,
    )
