"""FT — Django admin tarot card browsing.

Admin user can land on /admin/, see the Epic section's Tarot card +
DeckVariant rows, filter the changelist by deck variant, and open an
Earthman card's detail page to read its name + arcana group +
Fiorentine correspondence.

Split from the legacy test_component_cards_tarot.py (2026-05-12) so the
admin surface lives next to test_admin.py + test_admin_post_readonly.py;
the non-admin classes from that file migrated to test_room_tray.py.
"""
from selenium.webdriver.common.by import By

from apps.epic.models import DeckVariant
from apps.lyric.models import User

from .base import FunctionalTest


class TarotAdminTest(FunctionalTest):
    """Admin can browse tarot cards by deck variant via Django admin."""

    def setUp(self):
        super().setUp()
        from apps.epic.models import TarotCard
        # DeckVariant + TarotCard rows are flushed by TransactionTestCase — recreate
        self.earthman, _ = DeckVariant.objects.get_or_create(
            slug="earthman",
            defaults={"name": "Earthman Deck", "card_count": 108, "is_default": True},
        )
        # Seed enough cards so admin filter shows a meaningful count
        # The "108 tarot cards" assertion relies on deck_variant.card_count reported
        # by the admin, not on actual row count (admin shows real rows, so we seed
        # representative cards — 3 are enough to reach "The Schiz" in the list)
        for number, name, slug, group, correspondence in [
            (0,  "The Schiz",        "the-schiz-adm",       "",          "The Fool / Il Matto"),
            (1,  "Pope I: President","pope-i-president-adm","The Popes", "The Magician / Il Bagatto"),
            (50, "The Eagle",        "the-eagle-adm",       "",          "Judgement / L'Angelo"),
        ]:
            TarotCard.objects.get_or_create(
                deck_variant=self.earthman, slug=slug,
                defaults={
                    "name": name, "arcana": "MAJOR", "number": number,
                    "group": group, "correspondence": correspondence,
                },
            )
        self.superuser = User.objects.create_superuser(
            email="admin@example.com",
            password="correct-password",
        )

    def _login_to_admin(self):
        self.browser.get(self.live_server_url + "/admin/")
        self.wait_for(lambda: self.browser.find_element(By.ID, "id_username"))
        self.browser.find_element(By.ID, "id_username").send_keys("admin@example.com")
        self.browser.find_element(By.ID, "id_password").send_keys("correct-password")
        self.browser.find_element(By.CSS_SELECTOR, "input[type=submit]").click()
        # Wait for the POST → 302 → admin home to actually land before
        # returning — otherwise the caller's `browser.get(...)` can race
        # the in-flight form submit, hit /admin/login/?next=... and never
        # recover. Same flake class as 054b0aa fixed in test_admin.py.
        self.wait_for(lambda: self.assertIn(
            "Site administration",
            self.browser.find_element(By.TAG_NAME, "body").text,
        ))

    # ------------------------------------------------------------------ #
    # Test 1a — admin home lists Tarot cards + Deck variants under Epic   #
    # ------------------------------------------------------------------ #

    def test_admin_epic_section_shows_tarot_cards_and_deck_variants(self):
        self._login_to_admin()

        body = self.wait_for(lambda: self.browser.find_element(By.TAG_NAME, "body"))
        self.assertIn("Tarot cards", body.text)
        self.assertIn("Deck variants", body.text)

    # ------------------------------------------------------------------ #
    # Test 1b — changelist shows deck variant filter sidebar              #
    # ------------------------------------------------------------------ #

    def test_admin_tarot_card_list_shows_deck_variant_filter(self):
        self._login_to_admin()

        self.browser.get(self.live_server_url + "/admin/epic/tarotcard/")
        body = self.wait_for(lambda: self.browser.find_element(By.TAG_NAME, "body"))
        # Filter sidebar has a link for the Earthman deck
        self.assertIn("Earthman Deck", body.text)
        # Cards are listed — 3 seeded in setUp
        self.assertIn("3 tarot cards", body.text)

    # ------------------------------------------------------------------ #
    # Test 1c — Earthman card detail shows name, group, and correspondence #
    # ------------------------------------------------------------------ #

    def test_admin_earthman_card_detail_shows_group_and_correspondence(self):
        self._login_to_admin()

        self.browser.get(self.live_server_url + "/admin/epic/tarotcard/")
        self.wait_for(lambda: self.browser.find_element(By.TAG_NAME, "body"))

        # The Schiz is the Earthman Fool (card 0)
        self.browser.find_element(By.LINK_TEXT, "The Schiz").click()

        body = self.wait_for(lambda: self.browser.find_element(By.TAG_NAME, "body"))
        self.assertIn("Major Arcana", body.text)        # arcana dropdown
        self.assertIn("the-schiz-adm", body.text)       # slug (readonly → rendered as text)
        self.assertIn("The Fool / Il Matto", body.text) # correspondence (readonly → text)
