"""Tests for CLI commands using Click CliRunner."""
import pytest
from click.testing import CliRunner
from ayn_antivirus.cli import main


@pytest.fixture
def runner():
    return CliRunner()


def test_help(runner):
    result = runner.invoke(main, ["--help"])
    assert result.exit_code == 0
    assert "AYN Antivirus" in result.output or "scan" in result.output


def test_version(runner):
    result = runner.invoke(main, ["--version"])
    assert result.exit_code == 0
    assert "1.0.0" in result.output


def test_scan_help(runner):
    result = runner.invoke(main, ["scan", "--help"])
    assert result.exit_code == 0
    assert "--path" in result.output


def test_scan_containers_help(runner):
    result = runner.invoke(main, ["scan-containers", "--help"])
    assert result.exit_code == 0
    assert "--runtime" in result.output


def test_dashboard_help(runner):
    result = runner.invoke(main, ["dashboard", "--help"])
    assert result.exit_code == 0
    assert "--port" in result.output


def test_status(runner):
    result = runner.invoke(main, ["status"])
    assert result.exit_code == 0


def test_config_show(runner):
    result = runner.invoke(main, ["config", "--show"])
    assert result.exit_code == 0


def test_config_set_invalid_key(runner):
    result = runner.invoke(main, ["config", "--set", "evil_key", "value"])
    assert "Invalid config key" in result.output


def test_quarantine_list(runner):
    # May fail with PermissionError on systems without /var/lib/ayn-antivirus
    result = runner.invoke(main, ["quarantine", "list"])
    # Accept exit code 0 (success) or 1 (permission denied on default path)
    assert result.exit_code in (0, 1)


def test_update_help(runner):
    result = runner.invoke(main, ["update", "--help"])
    assert result.exit_code == 0


def test_fix_help(runner):
    result = runner.invoke(main, ["fix", "--help"])
    assert result.exit_code == 0
    assert "--dry-run" in result.output


def test_report_help(runner):
    result = runner.invoke(main, ["report", "--help"])
    assert result.exit_code == 0
    assert "--format" in result.output


def test_scan_processes_runs(runner):
    result = runner.invoke(main, ["scan-processes"])
    assert result.exit_code == 0


def test_scan_network_runs(runner):
    result = runner.invoke(main, ["scan-network"])
    assert result.exit_code == 0
