from django.test import SimpleTestCase

from apps.epic.utils import _planet_house, top_capacitors


class PlanetHouseFallbackTest(SimpleTestCase):
    def test_returns_1_when_no_cusp_matches(self):
        # Pathological cusps list: all 12 cusps identical (zero-width arcs).
        # No range has start < end, and the wrap-around condition is also
        # never satisfied, so the loop exhausts without returning — hitting
        # the fallback `return 1`.
        cusps = [0.0] * 12
        self.assertEqual(_planet_house(180.0, cusps), 1)

    def test_returns_1_when_degree_in_first_house_normal(self):
        # Standard, sequential cusps: degree=15 should land in house 1 (0–30).
        cusps = [i * 30.0 for i in range(12)]
        self.assertEqual(_planet_house(15.0, cusps), 1)


class TopCapacitorsTest(SimpleTestCase):
    """top_capacitors — capacitor names tied for the highest element count."""

    def test_returns_empty_when_elements_is_empty(self):
        self.assertEqual(top_capacitors({}), [])

    def test_returns_empty_when_elements_is_none(self):
        self.assertEqual(top_capacitors(None), [])

    def test_returns_empty_when_all_counts_are_zero(self):
        """All-zero counts (e.g. a brand-new chart with no planets) → empty list,
        not an Ardor-by-default. Exercises the `max(counts.values()) <= 0` branch."""
        self.assertEqual(
            top_capacitors({"Fire": 0, "Stone": 0, "Time": 0, "Space": 0, "Air": 0, "Water": 0}),
            [],
        )

    def test_returns_single_top_capacitor_when_one_element_wins(self):
        # Stone has highest count → Ossum
        result = top_capacitors({"Fire": 1, "Stone": 5, "Time": 2})
        self.assertEqual(result, ["Ossum"])

    def test_returns_multiple_capacitors_on_tie_in_clockwise_order(self):
        # Fire + Stone tied at 3 → order follows ELEMENT_ORDER (Fire first).
        result = top_capacitors({"Fire": 3, "Stone": 3, "Time": 2})
        self.assertEqual(result, ["Ardor", "Ossum"])

    def test_accepts_dict_values_with_count_key(self):
        """`elements` may carry enriched dicts like {"count": N, ...}."""
        result = top_capacitors({
            "Fire":  {"count": 1, "sign": "Aries"},
            "Stone": {"count": 4, "sign": "Taurus"},
        })
        self.assertEqual(result, ["Ossum"])
