"""
E2E tests for park browsing functionality.

These tests verify the complete user journey for browsing parks
using Playwright for browser automation.
"""

import pytest
from playwright.sync_api import Page, expect


@pytest.mark.e2e
class TestParkListPage:
    """E2E tests for park list page."""

    def test__park_list__displays_parks(self, page: Page, live_server, parks_data):
        """Test park list page displays parks."""
        page.goto(f"{live_server.url}/parks/")

        # Verify page title or heading
        expect(page.locator("h1")).to_be_visible()

        # Should display park cards or list items
        park_items = page.locator("[data-testid='park-card'], .park-item, .park-list-item")
        expect(park_items.first).to_be_visible()

    def test__park_list__shows_park_name(self, page: Page, live_server, parks_data):
        """Test park list shows park names."""
        page.goto(f"{live_server.url}/parks/")

        # First park should be visible
        first_park = parks_data[0]
        expect(page.get_by_text(first_park.name)).to_be_visible()

    def test__park_list__click_park__navigates_to_detail(self, page: Page, live_server, parks_data):
        """Test clicking a park navigates to detail page."""
        page.goto(f"{live_server.url}/parks/")

        first_park = parks_data[0]

        # Click on the park
        page.get_by_text(first_park.name).first.click()

        # Should navigate to detail page
        expect(page).to_have_url(f"**/{first_park.slug}/**")

    def test__park_list__search__filters_results(self, page: Page, live_server, parks_data):
        """Test search functionality filters parks."""
        page.goto(f"{live_server.url}/parks/")

        # Find search input
        search_input = page.locator("input[type='search'], input[name='q'], input[placeholder*='search' i]")

        if search_input.count() > 0:
            search_input.first.fill("E2E Test Park 0")

            # Wait for results to filter
            page.wait_for_timeout(500)

            # Should show only matching park
            expect(page.get_by_text("E2E Test Park 0")).to_be_visible()


@pytest.mark.e2e
class TestParkDetailPage:
    """E2E tests for park detail page."""

    def test__park_detail__displays_park_info(self, page: Page, live_server, parks_data):
        """Test park detail page displays park information."""
        park = parks_data[0]
        page.goto(f"{live_server.url}/parks/{park.slug}/")

        # Verify park name is displayed
        expect(page.get_by_role("heading", name=park.name)).to_be_visible()

    def test__park_detail__shows_rides_section(self, page: Page, live_server, parks_data):
        """Test park detail page shows rides section."""
        park = parks_data[0]
        page.goto(f"{live_server.url}/parks/{park.slug}/")

        # Look for rides section/tab
        page.locator("[data-testid='rides-section'], #rides, [role='tabpanel']")

        # Or a rides tab
        rides_tab = page.get_by_role("tab", name="Rides")

        if rides_tab.count() > 0:
            rides_tab.click()

        # Should show rides
        ride_items = page.locator(".ride-item, .ride-card, [data-testid='ride-item']")
        expect(ride_items.first).to_be_visible()

    def test__park_detail__shows_status(self, page: Page, live_server, parks_data):
        """Test park detail page shows park status."""
        park = parks_data[0]
        page.goto(f"{live_server.url}/parks/{park.slug}/")

        # Status badge or indicator should be visible
        status_indicator = page.locator(".status-badge, [data-testid='status'], .park-status")
        expect(status_indicator.first).to_be_visible()


@pytest.mark.e2e
class TestParkFiltering:
    """E2E tests for park filtering functionality."""

    def test__filter_by_status__updates_results(self, page: Page, live_server, parks_data):
        """Test filtering parks by status updates results."""
        page.goto(f"{live_server.url}/parks/")

        # Find status filter
        status_filter = page.locator("select[name='status'], [data-testid='status-filter']")

        if status_filter.count() > 0:
            status_filter.first.select_option("OPERATING")

            # Wait for results to update
            page.wait_for_timeout(500)

            # Results should be filtered

    def test__clear_filters__shows_all_parks(self, page: Page, live_server, parks_data):
        """Test clearing filters shows all parks."""
        page.goto(f"{live_server.url}/parks/")

        # Find clear filters button
        clear_btn = page.locator("[data-testid='clear-filters'], button:has-text('Clear')")

        if clear_btn.count() > 0:
            clear_btn.first.click()

            # Wait for results to update
            page.wait_for_timeout(500)


@pytest.mark.e2e
class TestParkNavigation:
    """E2E tests for park navigation."""

    def test__breadcrumb__navigates_back_to_list(self, page: Page, live_server, parks_data):
        """Test breadcrumb navigation back to park list."""
        park = parks_data[0]
        page.goto(f"{live_server.url}/parks/{park.slug}/")

        # Find breadcrumb
        breadcrumb = page.locator("nav[aria-label='breadcrumb'], .breadcrumb")

        if breadcrumb.count() > 0:
            # Click parks link in breadcrumb
            breadcrumb.get_by_role("link", name="Parks").click()

            expect(page).to_have_url("**/parks/**")

    def test__back_button__returns_to_previous_page(self, page: Page, live_server, parks_data):
        """Test browser back button returns to previous page."""
        page.goto(f"{live_server.url}/parks/")

        park = parks_data[0]
        page.get_by_text(park.name).first.click()

        # Wait for navigation
        page.wait_for_url(f"**/{park.slug}/**")

        # Go back
        page.go_back()

        expect(page).to_have_url("**/parks/**")
