"""Integration tests for CLI."""

from __future__ import annotations

import pytest

from electron_angle_patcher.cli import main, parse_arguments


class TestParseArguments:
    """Integration tests for argument parsing."""

    def test_no_arguments(self) -> None:
        """Test parsing with no arguments."""
        args = parse_arguments([])
        assert args.list_only is False
        assert args.deep_scan is False
        assert args.verbose is False

    def test_list_only_flag(self) -> None:
        """Test list-only flag."""
        args = parse_arguments(["--list-only"])
        assert args.list_only is True

    def test_dry_run_flag(self) -> None:
        """Test dry-run flag."""
        args = parse_arguments(["--dry-run"])
        assert args.dry_run is True

    def test_deep_scan_flag(self) -> None:
        """Test deep-scan flag."""
        args = parse_arguments(["--deep-scan"])
        assert args.deep_scan is True

    def test_verbose_flag(self) -> None:
        """Test verbose flag."""
        args = parse_arguments(["--verbose"])
        assert args.verbose is True

    def test_app_path(self) -> None:
        """Test app-path argument."""
        args = parse_arguments(["--app-path", "/path/to/Local State"])
        assert args.app_path == "/path/to/Local State"

    def test_app_name(self) -> None:
        """Test app-name argument."""
        args = parse_arguments(["--app-name", "MyApp"])
        assert args.app_name == "MyApp"

    def test_scan_dir(self) -> None:
        """Test scan-dir argument."""
        args = parse_arguments(["--scan-dir", "~/Applications"])
        assert args.scan_dir is not None

    def test_brave_path(self) -> None:
        """Test brave-path argument."""
        args = parse_arguments(["--brave-path", "/custom/Brave.app"])
        assert "Brave.app" in args.brave_path

    def test_lghub_path(self) -> None:
        """Test lghub-path argument."""
        args = parse_arguments(["--lghub-path", "/custom/lghub.app"])
        assert "lghub.app" in args.lghub_path

    def test_combined_flags(self) -> None:
        """Test multiple flags combined."""
        args = parse_arguments(["--list-only", "--verbose"])
        assert args.list_only is True
        assert args.verbose is True


class TestMain:
    """Integration tests for main CLI function."""

    @pytest.mark.skipif(
        "sys.platform != 'darwin'",
        reason="Platform check fails on non-macOS"
    )
    def test_main_help(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """Test --help doesn't crash."""
        import sys

        def mock_exit(code: int = 0) -> None:
            raise SystemExit(code)

        monkeypatch.setattr(sys, "exit", mock_exit)

        with pytest.raises(SystemExit):
            main(["--help"])

    @pytest.mark.skipif(
        "sys.platform == 'darwin'",
        reason="Platform check passes on macOS"
    )
    def test_main_platform_check(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """Test platform check on non-macOS."""
        import sys

        monkeypatch.setattr(sys, "platform", "linux")

        result = main([])
        assert result == 1  # Exit code for wrong platform

    @pytest.mark.unit
    def test_main_list_only_no_platform_check(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """Test list-only mode doesn't require platform (mocked)."""
        import sys

        # Mock platform
        monkeypatch.setattr(sys, "platform", "darwin")

        # Run with list-only to avoid actual patching
        result = main(["--list-only"])
        # Should succeed (no crash)
        assert result in (0, 1)  # May exit 0 or 1 depending on found apps
