"""Regression tests for the distinct Niryaana Shoola rule family."""

from __future__ import annotations

from datetime import datetime

from scripts.niryaana_shoola_dasha import (
    PROFILE_ID,
    calculate_niryaana_shoola_dasha,
    niryaana_shoola_applicability,
    select_niryaana_shoola_start_sign,
)


def test_niryaana_shoola_selects_stronger_second_or_eighth() -> None:
    second = niryaana_shoola_applicability(
        ascendant_longitude=0.0, second_strength=2.0, eighth_strength=1.0,
    )
    eighth = niryaana_shoola_applicability(
        ascendant_longitude=0.0, second_strength=1.0, eighth_strength=2.0,
    )
    assert second["starting_sign"] == "Taurus"
    assert second["direction"] == "reverse"
    assert eighth["starting_sign"] == "Scorpio"
    assert eighth["direction"] == "reverse"


def test_niryaana_shoola_uses_ayur_specific_rasi_strength() -> None:
    selection = select_niryaana_shoola_start_sign(
        ascendant_longitude=0.0,
        planet_longitudes={"Mars": 31.0, "Sun": 91.0},
    )

    assert selection["status"] == "selected"
    assert selection["selected_sign"] == "Taurus"
    assert selection["selection_levels"] == [
        "occupant_count",
        "all_planet_rasi_aspect_count",
        "exalted_occupant_presence",
    ]
    assert selection["candidates"][1]["occupants"] == ["Mars"]
    assert selection["candidates"][7]["rasi_aspecting_planets"] == ["Sun"]


def test_niryaana_shoola_counts_both_nodes_and_no_non_classical_body() -> None:
    occupancy = select_niryaana_shoola_start_sign(
        ascendant_longitude=0.0,
        planet_longitudes={"Rahu": 31.0, "Ketu": 211.0, "Pluto": 31.0},
    )
    aspects = select_niryaana_shoola_start_sign(
        ascendant_longitude=0.0,
        planet_longitudes={"Rahu": 1.0, "Ketu": 181.0},
    )

    assert occupancy["included_bodies"][-2:] == ["Rahu", "Ketu"]
    assert occupancy["candidates"][1]["occupants"] == ["Rahu"]
    assert occupancy["candidates"][7]["occupants"] == ["Ketu"]
    assert aspects["candidates"][1]["rasi_aspecting_planets"] == ["Ketu"]
    assert aspects["candidates"][7]["rasi_aspecting_planets"] == ["Rahu"]


def test_niryaana_shoola_blocks_unresolved_ayur_rasi_strength_tie() -> None:
    selection = select_niryaana_shoola_start_sign(
        ascendant_longitude=0.0,
        planet_longitudes={},
    )

    assert selection["status"] == "blocked"
    assert selection["reason"] == "niryaana_shoola_ayur_rasi_strength_tie_requires_source_rule"


def test_niryaana_shoola_blocks_equal_second_eighth_without_invented_tie_break() -> None:
    result = niryaana_shoola_applicability(
        ascendant_longitude=0.0, second_strength=1.0, eighth_strength=1.0,
    )
    assert result["execution_status"] == "blocked"
    assert "does not provide a tie-break" in result["reason"]


def test_niryaana_shoola_uses_modality_years_and_parity_direction() -> None:
    result = calculate_niryaana_shoola_dasha({
        "profile_id": PROFILE_ID,
        "birth_datetime": datetime(2000, 1, 1, 12, 0),
        "ascendant_longitude": 15.0,
        "second_strength": 2.0,
        "eighth_strength": 1.0,
    })
    assert result["execution_status"] == "executed"
    assert result["total_cycle"] == 96
    assert [row["sign"] for row in result["major"][:3]] == ["Taurus", "Aries", "Pisces"]
    assert [row["years"] for row in result["major"][:3]] == [8, 7, 9]
    assert result["period_depth_status"] == "mahadasha_executed_ad_pd_blocked_source_rule"
    assert result["antardasha"] == []
    assert result["relationship_applicability"] == "not_applicable_longevity_specific"


def test_legacy_goel_profile_is_blocked_as_misclassified() -> None:
    result = calculate_niryaana_shoola_dasha({
        "profile_id": "goel_nirayana_shoola_v1",
        "birth_datetime": datetime(2000, 1, 1, 12, 0),
        "ascendant_longitude": 0.0,
        "lagna_strength": 2.0,
        "seventh_strength": 1.0,
    })
    assert result["execution_status"] == "blocked"
    assert "conflates Niryaana Shoola with Shoola Dasha" in result["reason"]


def test_pyjhora_observation_profile_replays_same_native_rule_shape() -> None:
    result = calculate_niryaana_shoola_dasha({
        "profile_id": "pyjhora_2nd_8th_observation_v1",
        "birth_datetime": "2000-01-01T00:00:00",
        "ascendant_longitude": 15.0,
        "second_strength": 2.0,
        "eighth_strength": 1.0,
    })
    assert result["execution_status"] == "executed"
    assert result["starting_sign"] == "Taurus"
    assert result["profile_arbitration"]["selection_status"] == "explicit_profile_selected"
