"""Helper script for Robot Framework semantic validation suite tests.

Self-contained Python helper that loads and validates semantic validation
fixture suites (porting mismatches, dependency violations, API surface
changes, cross-file symbols, duplicate relative imports).  Prints
sentinel strings on success and exits with code 1 on failure.

Uses :mod:`features.fixtures.validation.suite_helpers` for shared
loading, lookup, and verification logic.
"""

from __future__ import annotations

import sys
from collections.abc import Callable
from pathlib import Path

# Ensure src and fixture helpers are importable when run from workspace root
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
_FIXTURES_DIR = str(
    Path(__file__).resolve().parents[1] / "features" / "fixtures" / "validation"
)
if _FIXTURES_DIR not in sys.path:
    sys.path.insert(0, _FIXTURES_DIR)

from suite_helpers import (  # noqa: E402, I001
    API_FILENAME,
    CIRCULAR_FILENAME,
    CROSS_FILE_FILENAME,
    DEPENDENCY_FILENAME,
    PORTING_FILENAME,
    load_fixtures,
    run_fixture,
    validate_fixture_schema,
    verify_fixture_result,
)


# ---------------------------------------------------------------------------
# Sub-commands
# ---------------------------------------------------------------------------


def test_porting_fixtures() -> None:
    """Load and verify all porting fixtures."""
    fixtures = load_fixtures(PORTING_FILENAME)
    assert len(fixtures) >= 6
    for f in fixtures:
        validate_fixture_schema(f)
    print("porting-fixtures-ok")


def test_dependency_fixtures() -> None:
    """Load and verify all dependency fixtures."""
    fixtures = load_fixtures(DEPENDENCY_FILENAME)
    assert len(fixtures) >= 6
    for f in fixtures:
        validate_fixture_schema(f)
    print("dependency-fixtures-ok")


def test_api_fixtures() -> None:
    """Load and verify all API surface fixtures."""
    fixtures = load_fixtures(API_FILENAME)
    assert len(fixtures) >= 6
    for f in fixtures:
        validate_fixture_schema(f)
    print("api-fixtures-ok")


def test_cross_file_fixtures() -> None:
    """Load and verify all cross-file fixtures."""
    fixtures = load_fixtures(CROSS_FILE_FILENAME)
    assert len(fixtures) >= 5
    for f in fixtures:
        validate_fixture_schema(f)
    print("cross-file-fixtures-ok")


def test_circular_fixtures() -> None:
    """Load and verify all duplicate import fixtures."""
    fixtures = load_fixtures(CIRCULAR_FILENAME)
    assert len(fixtures) >= 5
    for f in fixtures:
        validate_fixture_schema(f)
    print("circular-fixtures-ok")


def test_run_porting() -> None:
    """Run all porting fixtures through their expected rules."""
    fixtures = load_fixtures(PORTING_FILENAME)
    for fixture in fixtures:
        result = run_fixture(fixture)
        verify_fixture_result(fixture, result)
    print("run-porting-ok")


def test_run_dependency() -> None:
    """Run all dependency fixtures through their expected rules."""
    fixtures = load_fixtures(DEPENDENCY_FILENAME)
    for fixture in fixtures:
        result = run_fixture(fixture)
        verify_fixture_result(fixture, result)
    print("run-dependency-ok")


def test_run_api() -> None:
    """Run all API surface fixtures through their expected rules."""
    fixtures = load_fixtures(API_FILENAME)
    for fixture in fixtures:
        result = run_fixture(fixture)
        verify_fixture_result(fixture, result)
    print("run-api-ok")


def test_run_cross_file() -> None:
    """Run all cross-file fixtures through their expected rules."""
    fixtures = load_fixtures(CROSS_FILE_FILENAME)
    for fixture in fixtures:
        result = run_fixture(fixture)
        verify_fixture_result(fixture, result)
    print("run-cross-file-ok")


def test_run_circular() -> None:
    """Run all duplicate import fixtures through their expected rules."""
    fixtures = load_fixtures(CIRCULAR_FILENAME)
    for fixture in fixtures:
        result = run_fixture(fixture)
        verify_fixture_result(fixture, result)
    print("run-circular-ok")


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

_COMMANDS: dict[str, Callable[[], None]] = {
    "porting_fixtures": test_porting_fixtures,
    "dependency_fixtures": test_dependency_fixtures,
    "api_fixtures": test_api_fixtures,
    "cross_file_fixtures": test_cross_file_fixtures,
    "circular_fixtures": test_circular_fixtures,
    "run_porting": test_run_porting,
    "run_dependency": test_run_dependency,
    "run_api": test_run_api,
    "run_cross_file": test_run_cross_file,
    "run_circular": test_run_circular,
}

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print(
            "Usage: helper_semantic_validation_suite.py <command>",
            file=sys.stderr,
        )
        sys.exit(1)

    cmd = sys.argv[1]
    if cmd not in _COMMANDS:
        print(f"Unknown command: {cmd}", file=sys.stderr)
        sys.exit(1)

    try:
        _COMMANDS[cmd]()
    except Exception as exc:
        print(f"FAILED: {exc}", file=sys.stderr)
        sys.exit(1)
