"""Smoke tests for the Sanicode CLI.

Verifies that the CLI surface is importable and each subcommand's help
text is accessible. These tests do not exercise actual scan logic.
"""

from __future__ import annotations

import pytest
from typer.testing import CliRunner

from sanicode import __version__
from sanicode.cli import app

runner = CliRunner()


def test_help() -> None:
    """--help should exit 0 and mention the tool name."""
    result = runner.invoke(app, ["--help"])
    assert result.exit_code == 0, result.output
    assert "sanicode" in result.output.lower()


def test_version() -> None:
    """--version should print the current version string."""
    result = runner.invoke(app, ["--version"])
    assert result.exit_code == 0, result.output
    assert __version__ in result.output


@pytest.mark.parametrize(
    "subcommand",
    ["config", "scan", "report", "recommend", "graph", "serve"],
)
def test_subcommand_help(subcommand: str) -> None:
    """Each subcommand should respond to --help without error."""
    result = runner.invoke(app, [subcommand, "--help"])
    assert result.exit_code == 0, (
        f"'{subcommand} --help' exited with {result.exit_code}:\n{result.output}"
    )
    assert subcommand in result.output.lower()


# ---------------------------------------------------------------------------
# scan integration tests
# ---------------------------------------------------------------------------


def test_scan_detects_findings(tmp_path) -> None:
    """Scan a directory with known-bad code and verify findings appear in output."""
    bad_file = tmp_path / "bad.py"
    bad_file.write_text("eval(input())\n", encoding="utf-8")

    result = runner.invoke(app, ["scan", str(tmp_path)])
    assert result.exit_code == 0, (
        f"Unexpected exit code: {result.exit_code}\n{result.output}"
    )
    assert "Knowledge Graph" in result.output, (
        f"Expected 'Knowledge Graph' in output:\n{result.output}"
    )
    assert "SC001" in result.output, (
        f"Expected 'SC001' in output:\n{result.output}"
    )


def test_scan_nonexistent_path() -> None:
    """Scanning a path that does not exist should exit with code 1."""
    result = runner.invoke(app, ["scan", "/nonexistent/path/xyz"])
    assert result.exit_code == 1, (
        f"Expected exit code 1, got {result.exit_code}:\n{result.output}"
    )
    assert "does not exist" in result.output, (
        f"Expected 'does not exist' in output:\n{result.output}"
    )


def test_scan_clean_code_no_findings(tmp_path) -> None:
    """Scanning code with no bad patterns should report 0 findings."""
    clean_file = tmp_path / "clean.py"
    clean_file.write_text("x = 1 + 2\n", encoding="utf-8")

    result = runner.invoke(app, ["scan", str(tmp_path)])
    assert result.exit_code == 0, (
        f"Unexpected exit code: {result.exit_code}\n{result.output}"
    )
    assert "Findings:" in result.output, (
        f"Expected 'Findings:' in output:\n{result.output}"
    )
    # The count line reads "Findings: 0" (with possible Rich markup spacing).
    assert "0" in result.output, (
        f"Expected '0' findings count in output:\n{result.output}"
    )
