from __future__ import annotations

import argparse

import pytest

from zstarview.cli import args as cli_args


def _build_export_like_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser()
    cli_args.add_location_arguments(parser)
    cli_args.add_dataset_query_arguments(parser)
    cli_args.add_time_arguments(parser)
    cli_args.add_render_arguments(
        parser,
        include_window_geometry=False,
        include_sky_update_interval=False,
        include_startup_overlay_arguments=False,
    )
    return parser


def test_render_argument_helpers_can_build_parser_without_gui_only_options() -> None:
    parser = _build_export_like_parser()

    args = parser.parse_args(
        ["--place", " Matsue Station ", "--content-fov-deg", "110", "--theme", "day"]
    )
    cli_args._normalize_location_arguments(parser, args)

    assert args.place == "Matsue Station"
    assert args.content_fov_deg == 110.0
    assert args.theme == "day"
    assert not hasattr(args, "window_geometry")
    assert not hasattr(args, "sky_update_interval")
    assert not hasattr(args, "show_dso_initial")
    assert not hasattr(args, "show_asterisms_initial")


def test_dataset_query_validation_works_with_parser_missing_gui_only_options() -> None:
    parser = _build_export_like_parser()
    args = parser.parse_args(["--list-viewpoints", "t", "--theme", "day"])

    cli_args._normalize_dataset_query_arguments(parser, args)

    with pytest.raises(SystemExit):
        cli_args._validate_dataset_query_compatibility(parser, args)


def test_main_help_text_is_ascii_only_for_windows_consoles() -> None:
    help_text = cli_args.build_main_argument_parser().format_help()

    assert all(ord(ch) < 128 for ch in help_text)


def test_main_parser_version_option_prints_package_version(
    capsys: pytest.CaptureFixture[str],
) -> None:
    with pytest.raises(SystemExit) as exc_info:
        cli_args.parse_args(["--version"])

    captured = capsys.readouterr()

    assert exc_info.value.code == 0
    assert captured.out.endswith(f" {cli_args.__version__}\n")
    assert captured.err == ""


def test_parse_args_clamps_vmag_limit_to_committed_catalog_max() -> None:
    args = cli_args.parse_args(["-V", "11", "Matsue"])

    assert args.vmag_limit == 10.5


def test_parse_args_accepts_clear_long_lived_cache() -> None:
    args = cli_args.parse_args(["--clear-long-lived-cache", "Matsue"])

    assert args.clear_long_lived_cache is True
