"""Smoke tests: package importable, version exposed, CLI invokable."""

from __future__ import annotations

from pathlib import Path

from click.testing import CliRunner

import cerbero_bite
from cerbero_bite import __version__
from cerbero_bite.cli import main as cli_main


def test_package_version_string() -> None:
    assert isinstance(__version__, str)
    assert __version__.count(".") == 2


def test_package_module_attribute() -> None:
    assert cerbero_bite.__version__ == __version__


def test_cli_help_lists_status_command() -> None:
    runner = CliRunner()
    result = runner.invoke(cli_main, ["--help"])
    assert result.exit_code == 0
    assert "status" in result.output


def test_cli_status_when_state_missing(tmp_data_dir: Path) -> None:
    runner = CliRunner()
    result = runner.invoke(
        cli_main,
        [
            "--log-dir",
            str(tmp_data_dir / "log"),
            "status",
            "--db",
            str(tmp_data_dir / "missing.sqlite"),
        ],
    )
    assert result.exit_code == 0
    assert "Cerbero Bite" in result.output
    assert "never started" in result.output


def test_cli_status_after_kill_switch_arm(tmp_data_dir: Path) -> None:
    runner = CliRunner()
    db_path = tmp_data_dir / "state.sqlite"
    audit_path = tmp_data_dir / "audit.log"
    runner.invoke(
        cli_main,
        [
            "--log-dir", str(tmp_data_dir / "log"),
            "kill-switch", "arm",
            "--reason", "smoke",
            "--db", str(db_path),
            "--audit", str(audit_path),
        ],
    )
    result = runner.invoke(cli_main, ["status", "--db", str(db_path)])
    assert result.exit_code == 0
    assert "ARMED" in result.output
    assert "open positions: 0" in result.output


def test_cli_kill_switch_arm_persists_state(tmp_data_dir: Path) -> None:
    runner = CliRunner()
    db_path = tmp_data_dir / "state.sqlite"
    audit_path = tmp_data_dir / "audit.log"
    result = runner.invoke(
        cli_main,
        [
            "--log-dir",
            str(tmp_data_dir / "log"),
            "kill-switch",
            "arm",
            "--reason",
            "smoke",
            "--db",
            str(db_path),
            "--audit",
            str(audit_path),
        ],
    )
    assert result.exit_code == 0, result.output
    assert "ARMED" in result.output
    assert db_path.exists()
    assert audit_path.exists()


def test_cli_version_flag() -> None:
    runner = CliRunner()
    result = runner.invoke(cli_main, ["--version"])
    assert result.exit_code == 0
    assert __version__ in result.output
