from django.template import Context, Template
from django.test import TestCase


def render_tooltip(data):
    """Helper: render the {% tooltip %} tag with the given data dict."""
    tpl = Template("{% load tooltip_tags %}{% tooltip data %}")
    return tpl.render(Context({'data': data}))


class TooltipTagRequiredFieldsTest(TestCase):

    def test_renders_title(self):
        html = render_tooltip({'title': 'Sun'})
        self.assertIn('Sun', html)
        self.assertIn('class="tt-title"', html)

    def test_wrapper_has_tt_class(self):
        html = render_tooltip({'title': 'Moon'})
        self.assertIn('class="tt"', html)


class TooltipTagOptionalTextFieldsTest(TestCase):

    def test_renders_type_label_when_present(self):
        html = render_tooltip({'title': 'Sun', 'type_label': 'Planet'})
        self.assertIn('Planet', html)
        self.assertIn('tt-type', html)

    def test_omits_type_label_when_absent(self):
        html = render_tooltip({'title': 'Sun'})
        self.assertNotIn('tt-type', html)

    def test_renders_symbol_when_present(self):
        html = render_tooltip({'title': 'Sun', 'symbol': '☉'})
        self.assertIn('☉', html)
        self.assertIn('tt-symbol', html)

    def test_omits_symbol_when_absent(self):
        html = render_tooltip({'title': 'Sun'})
        self.assertNotIn('tt-symbol', html)

    def test_renders_degree_str_when_present(self):
        html = render_tooltip({'title': 'Sun', 'degree_str': '14° 22′ Scorpio'})
        self.assertIn('14° 22′ Scorpio', html)
        self.assertIn('tt-degree', html)

    def test_omits_degree_str_when_absent(self):
        html = render_tooltip({'title': 'Sun'})
        self.assertNotIn('tt-degree', html)

    def test_renders_description_when_present(self):
        html = render_tooltip({'title': 'Sun', 'description': 'Planet of vitality.'})
        self.assertIn('Planet of vitality.', html)
        self.assertIn('tt-description', html)

    def test_renders_shoptalk_in_em_when_present(self):
        html = render_tooltip({'title': 'Sun', 'shoptalk': 'Ruler of Leo.'})
        self.assertIn('Ruler of Leo.', html)
        self.assertIn('tt-shoptalk', html)
        self.assertIn('<em>', html)

    def test_renders_expiry_when_present(self):
        html = render_tooltip({'title': 'Pass', 'expiry': 'no expiry'})
        self.assertIn('no expiry', html)
        self.assertIn('tt-expiry', html)

    def test_renders_effect_when_present(self):
        html = render_tooltip({'title': 'Pass', 'effect': 'Admit All Entry'})
        self.assertIn('Admit All Entry', html)
        self.assertIn('tt-effect', html)


class TooltipTagExtrasTest(TestCase):

    def test_renders_dignities_table_when_present(self):
        data = {
            'title': 'Saturn',
            'extras': {
                'dignities': {
                    'Domicile': 'Capricorn',
                    'Exalted': 'Libra',
                },
            },
        }
        html = render_tooltip(data)
        self.assertIn('tt-table--dignities', html)
        self.assertIn('Capricorn', html)
        self.assertIn('Exalted', html)

    def test_omits_dignities_when_absent(self):
        html = render_tooltip({'title': 'Sun', 'extras': {}})
        self.assertNotIn('tt-table--dignities', html)

    def test_renders_aspects_list_when_present(self):
        data = {
            'title': 'Mercury',
            'extras': {
                'aspects': [
                    {'symbol': '△', 'type': 'Trine', 'body': 'Venus', 'orb': '2° 14′'},
                ],
            },
        }
        html = render_tooltip(data)
        self.assertIn('tt-aspects', html)
        self.assertIn('Trine', html)
        self.assertIn('Venus', html)

    def test_omits_aspects_when_absent(self):
        html = render_tooltip({'title': 'Sun', 'extras': {}})
        self.assertNotIn('tt-aspects', html)

    def test_renders_keyword_lists_when_present(self):
        data = {
            'title': 'The Schizo',
            'extras': {
                'keywords_up': ['willpower', 'skill'],
                'keywords_rev': ['hubris'],
            },
        }
        html = render_tooltip(data)
        self.assertIn('tt-keywords', html)
        self.assertIn('willpower', html)
        self.assertIn('hubris', html)

    def test_omits_keywords_when_absent(self):
        html = render_tooltip({'title': 'Sun', 'extras': {}})
        self.assertNotIn('tt-keywords', html)

    def test_renders_fyi_cautions_section_when_present(self):
        data = {
            'title': 'The Schizo',
            'extras': {
                'cautions': [
                    {
                        'title': 'Caution!',
                        'type_label': 'Rival Interaction',
                        'shoptalk': '[Shoptalk forthcoming]',
                        'effect': 'This card will reverse...',
                    },
                ],
            },
        }
        html = render_tooltip(data)
        self.assertIn('tt-fyi', html)
        self.assertIn('tt-fyi--cautions', html)
        self.assertIn('Rival Interaction', html)

    def test_omits_cautions_section_when_absent(self):
        html = render_tooltip({'title': 'Sun', 'extras': {}})
        self.assertNotIn('tt-fyi--cautions', html)

    def test_renders_nav_when_present(self):
        data = {
            'title': 'House of Self',
            'extras': {
                'nav': {'prv': 'house-12', 'nxt': 'house-02'},
            },
        }
        html = render_tooltip(data)
        self.assertIn('tt-nav', html)
        self.assertIn('house-02', html)
        self.assertIn('house-12', html)

    def test_omits_nav_when_absent(self):
        html = render_tooltip({'title': 'Sun', 'extras': {}})
        self.assertNotIn('tt-nav', html)

    def test_no_extras_key_does_not_error(self):
        """Tag should handle data with no 'extras' key gracefully."""
        html = render_tooltip({'title': 'Sun'})
        self.assertIn('Sun', html)
