# app/modules/loyalty/tests/integration/test_store_pages.py
"""
Integration tests for store loyalty page routes (HTML rendering).

Tests the store page routes at:
    /store/{store_code}/loyalty (redirect to terminal)
    /store/{store_code}/loyalty/terminal
    /store/{store_code}/loyalty/cards
    /store/{store_code}/loyalty/cards/{card_id}
    /store/{store_code}/loyalty/program
    /store/{store_code}/loyalty/program/edit
    /store/{store_code}/loyalty/analytics
    /store/{store_code}/loyalty/enroll

Authentication: Uses real JWT login via loyalty_store_headers fixture.
"""

import pytest

# ============================================================================
# Helper
# ============================================================================


def _base(store_code: str) -> str:
    return f"/store/{store_code}/loyalty"


# ============================================================================
# Loyalty Root (Redirect)
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreLoyaltyRoot:
    """Tests for GET /store/{store_code}/loyalty — redirects to terminal."""

    def test_root_redirects_to_terminal(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Root loyalty URL redirects to terminal."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            _base(store_code),
            headers=loyalty_store_headers,
            follow_redirects=False,
        )
        assert response.status_code == 302
        assert "/loyalty/terminal" in response.headers["location"]

    def test_root_requires_auth(self, client, loyalty_store_setup):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            _base(store_code),
            follow_redirects=False,
        )
        assert response.status_code in [401, 403]


# ============================================================================
# Terminal Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreTerminalPage:
    """Tests for GET /store/{store_code}/loyalty/terminal."""

    def test_terminal_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Terminal page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            f"{_base(store_code)}/terminal",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_terminal_page_requires_auth(self, client, loyalty_store_setup):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(f"{_base(store_code)}/terminal")
        assert response.status_code in [401, 403]


# ============================================================================
# Cards (Members List) Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreCardsPage:
    """Tests for GET /store/{store_code}/loyalty/cards."""

    def test_cards_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Cards page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            f"{_base(store_code)}/cards",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_cards_page_requires_auth(self, client, loyalty_store_setup):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(f"{_base(store_code)}/cards")
        assert response.status_code in [401, 403]


# ============================================================================
# Card Detail Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreCardDetailPage:
    """Tests for GET /store/{store_code}/loyalty/cards/{card_id}."""

    def test_card_detail_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Card detail page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        card_id = loyalty_store_setup["card"].id
        response = client.get(
            f"{_base(store_code)}/cards/{card_id}",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_card_detail_page_requires_auth(
        self, client, loyalty_store_setup
    ):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        card_id = loyalty_store_setup["card"].id
        response = client.get(f"{_base(store_code)}/cards/{card_id}")
        assert response.status_code in [401, 403]


# ============================================================================
# Program View Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreProgramPage:
    """Tests for GET /store/{store_code}/loyalty/program."""

    def test_program_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Program page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            f"{_base(store_code)}/program",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_program_page_requires_auth(self, client, loyalty_store_setup):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(f"{_base(store_code)}/program")
        assert response.status_code in [401, 403]


# ============================================================================
# Program Edit Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreProgramEditPage:
    """Tests for GET /store/{store_code}/loyalty/program/edit."""

    def test_program_edit_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Program edit page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            f"{_base(store_code)}/program/edit",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_program_edit_page_requires_auth(
        self, client, loyalty_store_setup
    ):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(f"{_base(store_code)}/program/edit")
        assert response.status_code in [401, 403]


# ============================================================================
# Analytics Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreAnalyticsPage:
    """Tests for GET /store/{store_code}/loyalty/analytics."""

    def test_analytics_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Analytics page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            f"{_base(store_code)}/analytics",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_analytics_page_requires_auth(self, client, loyalty_store_setup):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(f"{_base(store_code)}/analytics")
        assert response.status_code in [401, 403]


# ============================================================================
# Enrollment Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStoreEnrollPage:
    """Tests for GET /store/{store_code}/loyalty/enroll."""

    def test_enroll_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """Enrollment page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            f"{_base(store_code)}/enroll",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_enroll_page_requires_auth(self, client, loyalty_store_setup):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(f"{_base(store_code)}/enroll")
        assert response.status_code in [401, 403]


# ============================================================================
# Staff PINs Page
# ============================================================================


@pytest.mark.integration
@pytest.mark.loyalty
class TestStorePinsPage:
    """Tests for GET /store/{store_code}/loyalty/pins."""

    def test_pins_page_renders(
        self, client, loyalty_store_headers, loyalty_store_setup
    ):
        """PINs page returns HTML."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(
            f"{_base(store_code)}/pins",
            headers=loyalty_store_headers,
        )
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]

    def test_pins_page_requires_auth(self, client, loyalty_store_setup):
        """Unauthenticated request is rejected."""
        store_code = loyalty_store_setup["store"].subdomain
        response = client.get(f"{_base(store_code)}/pins")
        assert response.status_code in [401, 403]
