"""
Tests for the token_diet.cli module using Click's test runner.
"""

from __future__ import annotations

from click.testing import CliRunner

from token_diet.cli import main


LONG_PROMPT = (
    "Please take the following document and create a comprehensive summary "
    "that covers all of the main points and key details. I would like you to "
    "make sure to include every important fact. In order to do this effectively, "
    "please go through the text very carefully. It is important that you do not "
    "miss anything. Could you please also make sure to keep the summary concise? "
    "Due to the fact that the document is long, a good summary is essential. "
    "Please note that accuracy is very important. I would like you to include "
    "all of the relevant information in order to produce a thorough summary. "
    "At this point in time, the document has not been summarized. "
) * 3


SHORT_PROMPT = "Summarize this."


class TestCLIBasic:
    def test_help(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, ["--help"])
        assert result.exit_code == 0
        assert "token" in result.output.lower() or "compress" in result.output.lower()

    def test_no_input_exits_nonzero(self) -> None:
        runner = CliRunner()
        # Provide a closed stdin so it's not a tty and not piped either
        result = runner.invoke(main, [], input="")
        assert result.exit_code != 0

    def test_stdin_long_prompt(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, [], input=LONG_PROMPT)
        assert result.exit_code == 0
        assert "TOKEN DIET" in result.output

    def test_file_input(self, tmp_path) -> None:  # type: ignore[no-untyped-def]
        p = tmp_path / "prompt.txt"
        p.write_text(LONG_PROMPT)
        runner = CliRunner()
        result = runner.invoke(main, [str(p)])
        assert result.exit_code == 0
        assert "TOKEN DIET" in result.output

    def test_missing_file_exits_nonzero(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, ["/nonexistent/path/prompt.txt"])
        assert result.exit_code != 0

    def test_quiet_flag_outputs_only_text(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, ["--quiet"], input=LONG_PROMPT)
        assert result.exit_code == 0
        # Rich report headers should NOT appear
        assert "TOKEN DIET" not in result.output
        assert "Results" not in result.output
        # But there should be actual content
        assert len(result.output.strip()) > 0

    def test_diff_flag(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, ["--diff"], input=LONG_PROMPT)
        assert result.exit_code == 0
        assert "DIFF" in result.output

    def test_level_gentle(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, ["--level", "gentle"], input=LONG_PROMPT)
        assert result.exit_code == 0
        assert "gentle" in result.output

    def test_level_aggressive(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, ["--level", "aggressive"], input=LONG_PROMPT)
        assert result.exit_code == 0
        assert "aggressive" in result.output

    def test_short_prompt_reports_nothing_to_compress(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, [], input=SHORT_PROMPT)
        assert result.exit_code == 0
        # Should mention it's already short
        assert "50" in result.output or "nothing" in result.output.lower()

    def test_version(self) -> None:
        runner = CliRunner()
        result = runner.invoke(main, ["--version"])
        assert result.exit_code == 0
        # version string contains at least a digit
        assert any(ch.isdigit() for ch in result.output)
