"""Validate bilingual UI text resources."""

from __future__ import annotations

import ast
from pathlib import Path

import pytest


def _load_ui_texts() -> dict[str, dict[str, str]]:
    """Parse the modern GUI file to extract the _ui_texts dictionary."""
    source_path = Path("web_sentinel/gui/modern_tabbed_window.py")
    source = source_path.read_text(encoding="utf-8")
    module = ast.parse(source, filename=str(source_path))

    class _UiTextsFinder(ast.NodeVisitor):
        def __init__(self) -> None:
            self.value = None

        def visit_Assign(self, node: ast.Assign) -> None:  # noqa: N802
            for target in node.targets:
                if (
                    isinstance(target, ast.Attribute)
                    and target.attr == "_ui_texts"
                    and isinstance(node.value, (ast.Dict))
                ):
                    self.value = ast.literal_eval(node.value)  # type: ignore[arg-type]

    finder = _UiTextsFinder()
    finder.visit(module)
    if finder.value is None:
        raise AssertionError("Unable to locate _ui_texts dictionary in modern_tabbed_window.py")
    return finder.value


@pytest.fixture(scope="module")
def ui_texts() -> dict[str, dict[str, str]]:
    """Return the parsed UI texts dictionary."""
    return _load_ui_texts()


def test_ui_texts_have_bilingual_entries(ui_texts: dict[str, dict[str, str]]) -> None:
    """Ensure every UI text provides both French and English versions."""
    for key, value in ui_texts.items():
        assert isinstance(value, dict), f"Entry for {key} must be a dict"
        assert "fr" in value, f"Missing French translation for {key}"
        assert "en" in value, f"Missing English translation for {key}"
        assert isinstance(value["fr"], str) and value["fr"], f"Empty French value for {key}"
        assert isinstance(value["en"], str) and value["en"], f"Empty English value for {key}"


@pytest.mark.parametrize(
    "key",
    [
        "domain_scan_running_title",
        "domain_scan_running_body",
        "domain_missing_body",
        "domain_limit_body",
        "domain_invasive_warning",
        "domain_scan_error",
        "status_scan_running",
        "status_scan_progress",
        "status_scan_done",
        "status_scan_error",
        "scan_started_line",
        "domain_limit_title",
        "dialog_export_results_json",
        "dialog_export_results_html",
        "export_no_results",
        "export_no_report",
        "export_results_success",
        "export_results_error",
        "login_error_message",
        "logout_error_message",
        "login_success_title",
        "login_success_message",
        "logout_success_message",
        "dialog_export_sast_json",
        "dialog_export_sast_html",
        "sast_info_limits",
        "sast_scan_incomplete_body",
        "status_no_source",
    ],
)
def test_expected_keys_present(ui_texts: dict[str, dict[str, str]], key: str) -> None:
    """Specific keys required by the GUI must be present in both languages."""
    assert key in ui_texts, f"Missing UI text entry for {key}"
