from playwright.sync_api import Page, expect


def test_rides_list_page(page: Page):
    # Navigate to rides page
    page.goto("http://localhost:8000/rides/")

    # Check rides list elements
    expect(page.get_by_role("heading", name="Rides & Attractions")).to_be_visible()
    expect(page.get_by_role("searchbox", name="Search rides")).to_be_visible()

    # Check filter options
    expect(page.get_by_role("combobox", name="Park")).to_be_visible()
    expect(page.get_by_role("combobox", name="Type")).to_be_visible()
    expect(page.get_by_role("combobox", name="Manufacturer")).to_be_visible()
    expect(page.get_by_role("combobox", name="Status")).to_be_visible()


def test_ride_search(page: Page):
    # Navigate to rides page
    page.goto("http://localhost:8000/rides/")

    # Search for a ride
    search_box = page.get_by_role("searchbox", name="Search rides")
    search_box.fill("Space Mountain")
    search_box.press("Enter")

    # Verify search results
    expect(page.get_by_text("Space Mountain")).to_be_visible()
    expect(page.get_by_text("Walt Disney World")).to_be_visible()


def test_ride_filters(page: Page):
    # Navigate to rides page
    page.goto("http://localhost:8000/rides/")

    # Select park filter
    page.get_by_role("combobox", name="Park").select_option("Walt Disney World")

    # Select type filter
    page.get_by_role("combobox", name="Type").select_option("Roller Coaster")

    # Select manufacturer filter
    page.get_by_role("combobox", name="Manufacturer").select_option("Vekoma")

    # Verify filtered results
    expect(page.get_by_text("Space Mountain")).to_be_visible()
    expect(page.get_by_text("Roller Coaster")).to_be_visible()
    expect(page.get_by_text("Vekoma")).to_be_visible()


def test_ride_detail_page(page: Page):
    # Navigate to a specific ride page
    page.goto("http://localhost:8000/rides/space-mountain-magic-kingdom/")

    # Check ride details
    expect(page.get_by_role("heading", name="Space Mountain")).to_be_visible()
    expect(page.get_by_text("Park:")).to_be_visible()
    expect(page.get_by_text("Walt Disney World")).to_be_visible()
    expect(page.get_by_text("Type:")).to_be_visible()
    expect(page.get_by_text("Roller Coaster")).to_be_visible()
    expect(page.get_by_text("Manufacturer:")).to_be_visible()
    expect(page.get_by_text("Vekoma")).to_be_visible()

    # Check ride sections
    expect(page.get_by_role("tab", name="Overview")).to_be_visible()
    expect(page.get_by_role("tab", name="Stats")).to_be_visible()
    expect(page.get_by_role("tab", name="Reviews")).to_be_visible()
    expect(page.get_by_role("tab", name="Photos")).to_be_visible()


def test_ride_stats(page: Page):
    # Navigate to ride page
    page.goto("http://localhost:8000/rides/space-mountain-magic-kingdom/")

    # Click on Stats tab
    page.get_by_role("tab", name="Stats").click()

    # Check stats are visible
    expect(page.get_by_text("Height:")).to_be_visible()
    expect(page.get_by_text("Length:")).to_be_visible()
    expect(page.get_by_text("Speed:")).to_be_visible()
    expect(page.get_by_text("Duration:")).to_be_visible()
    expect(page.get_by_text("Height Requirement:")).to_be_visible()


def test_add_ride_review(page: Page):
    # First login
    page.goto("http://localhost:8000/accounts/login/")
    page.get_by_label("Username").fill("testuser")
    page.get_by_label("Password").fill("testpass123")
    page.get_by_role("button", name="Sign In").click()

    # Navigate to ride page
    page.goto("http://localhost:8000/rides/space-mountain-magic-kingdom/")

    # Click on Reviews tab
    page.get_by_role("tab", name="Reviews").click()

    # Click Write Review button
    page.get_by_role("button", name="Write Review").click()

    # Fill review form
    page.get_by_label("Rating").select_option("5")
    page.get_by_label("Title").fill("Best Coaster Ever")
    page.get_by_label("Review").fill("Such a thrilling experience in the dark!")

    # Submit review
    page.get_by_role("button", name="Submit Review").click()

    # Verify review appears
    expect(page.get_by_text("Best Coaster Ever")).to_be_visible()
    expect(page.get_by_text("Such a thrilling experience in the dark!")).to_be_visible()


def test_add_ride_photo(page: Page, test_images):
    # First login
    page.goto("http://localhost:8000/accounts/login/")
    page.get_by_label("Username").fill("testuser")
    page.get_by_label("Password").fill("testpass123")
    page.get_by_role("button", name="Sign In").click()

    # Navigate to ride page
    page.goto("http://localhost:8000/rides/space-mountain-magic-kingdom/")

    # Click on Photos tab
    page.get_by_role("tab", name="Photos").click()

    # Click Add Photo button
    page.get_by_role("button", name="Add Photo").click()

    # Upload photo
    page.get_by_label("Photo").set_input_files(test_images["test_photo"])
    page.get_by_label("Caption").fill("Awesome ride entrance")

    # Submit photo
    page.get_by_role("button", name="Upload Photo").click()

    # Verify photo appears
    expect(page.get_by_text("Awesome ride entrance")).to_be_visible()


def test_ride_wait_times(page: Page):
    # Navigate to ride page
    page.goto("http://localhost:8000/rides/space-mountain-magic-kingdom/")

    # Check wait time section
    expect(page.get_by_text("Current Wait Time:")).to_be_visible()
    expect(page.get_by_text("minutes")).to_be_visible()

    # Check historical wait times
    expect(page.get_by_text("Average Wait Times")).to_be_visible()
    expect(page.locator("#wait-time-chart")).to_be_visible()


def test_ride_location(page: Page):
    # Navigate to ride page
    page.goto("http://localhost:8000/rides/space-mountain-magic-kingdom/")

    # Check location section
    expect(page.get_by_text("Location in Park")).to_be_visible()
    expect(page.locator("#ride-location-map")).to_be_visible()

    # Verify map marker
    expect(page.locator(".ride-marker")).to_be_visible()
