"""Tests for MCP server tool functions."""

from unittest.mock import patch

from ars25.server import (
    search_statutes,
    get_statute,
    list_statutes,
    get_chapter_tree,
    ingest_statutes,
    get_ingestion_status,
)


def test_search_statutes_no_db():
    with patch("ars25.server.table_exists", return_value=False):
        result = search_statutes("child custody")
    assert result["status"] == "error"
    assert "not initialized" in result["error"].lower()


def test_get_statute_invalid_section():
    with patch("ars25.server.table_exists", return_value=True):
        result = get_statute("invalid")
    assert result["status"] == "error"
    assert result["error_type"] == "validation_error"


def test_get_statute_no_db():
    with patch("ars25.server.table_exists", return_value=False):
        result = get_statute("25-403")
    assert result["status"] == "error"


def test_list_statutes_no_db():
    with patch("ars25.server.table_exists", return_value=False):
        result = list_statutes()
    assert result["status"] == "error"


def test_get_chapter_tree_no_db():
    with patch("ars25.server.table_exists", return_value=False):
        result = get_chapter_tree()
    assert result["status"] == "error"


def test_ingest_statutes_skip_existing():
    with patch("ars25.server.table_exists", return_value=True), \
         patch("ars25.ingestion.pipeline.table_exists", return_value=True):
        result = ingest_statutes(force=False)
    assert result["status"] == "success"
    assert result["skipped"] is True


def test_get_ingestion_status_no_db():
    with patch("ars25.server.table_exists", return_value=False):
        result = get_ingestion_status()
    assert result["status"] == "success"
    assert result["initialized"] is False
