# tests/test_validate_svd_themes.py
"""Tests for scripts/validate_svd_themes.py."""

import pytest


def test_check_canonical_right_on_right_no_left_right_pole():
    """Validation should work without left_pole/right_pole in themes."""
    import sys

    sys.path.insert(0, ".")
    from scripts.validate_svd_themes import check_canonical_right_on_right

    # Mock party positions
    party_positions = {
        "PVV": {1: 0.5},
        "FVD": {1: 0.4},
        "SP": {1: -0.5},
        "GroenLinks-PvdA": {1: -0.4},
    }
    party_avg_vectors = {
        "PVV": [0.5],
        "FVD": [0.4],
        "SP": [-0.5],
        "GroenLinks-PvdA": [-0.4],
    }

    # Themes WITHOUT left_pole/right_pole
    themes = {
        1: {
            "label": "Test axis",
            "positive_pole": "Right",
            "negative_pole": "Left",
            "flip": False,
        }
    }

    canonical_right = frozenset(["PVV", "FVD"])
    canonical_left = frozenset(["SP", "GroenLinks-PvdA"])

    divergences = check_canonical_right_on_right(
        party_positions,
        party_avg_vectors,
        themes,
        canonical_right,
        canonical_left,
        num_components=1,
    )

    # Should return empty list (no divergences) because right parties are on right
    assert divergences == []


def test_check_canonical_right_on_right_detects_divergence():
    """Validation should detect when right parties are NOT on right."""
    import sys

    sys.path.insert(0, ".")
    from scripts.validate_svd_themes import check_canonical_right_on_right

    # Mock party positions where right parties are on LEFT
    # The flip will be computed as True (since right_mean=-0.45 < left_mean=0.45)
    # After flip: right parties will be on the RIGHT side (correct)
    # So no divergence in this case - the flip mechanism works
    party_positions = {
        "PVV": {1: -0.5},  # Right party on left side
        "FVD": {1: -0.4},
        "SP": {1: 0.5},  # Left party on right side
        "GroenLinks-PvdA": {1: 0.4},
    }
    party_avg_vectors = {
        "PVV": [-0.5],
        "FVD": [-0.4],
        "SP": [0.5],
        "GroenLinks-PvdA": [0.4],
    }

    themes = {
        1: {
            "label": "Test axis",
            "positive_pole": "Right",
            "negative_pole": "Left",
            "flip": False,  # Will be overridden by compute_flip_direction
        }
    }

    canonical_right = frozenset(["PVV", "FVD"])
    canonical_left = frozenset(["SP", "GroenLinks-PvdA"])

    divergences = check_canonical_right_on_right(
        party_positions,
        party_avg_vectors,
        themes,
        canonical_right,
        canonical_left,
        num_components=1,
    )

    # No divergence because flip=True was computed and corrected the issue
    assert divergences == []


def test_check_canonical_right_on_right_missing_party_data():
    """Validation should detect when canonical party data is missing."""
    import sys

    sys.path.insert(0, ".")
    from scripts.validate_svd_themes import check_canonical_right_on_right

    # Empty party positions - no data for canonical parties
    party_positions = {}
    party_avg_vectors = {}

    themes = {
        1: {
            "label": "Test axis",
            "positive_pole": "Right",
            "negative_pole": "Left",
            "flip": False,
        }
    }

    canonical_right = frozenset(["PVV", "FVD"])
    canonical_left = frozenset(["SP", "GroenLinks-PvdA"])

    divergences = check_canonical_right_on_right(
        party_positions,
        party_avg_vectors,
        themes,
        canonical_right,
        canonical_left,
        num_components=1,
    )

    # Should detect missing party data
    assert len(divergences) == 1
    assert divergences[0]["issue"] == "missing_canonical_party_data"
