from playwright.sync_api import Page, expect


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

    # Check parks list elements
    expect(page.get_by_role("heading", name="Theme Parks")).to_be_visible()
    expect(page.get_by_role("searchbox", name="Search parks")).to_be_visible()

    # Check filter options
    expect(page.get_by_role("combobox", name="Country")).to_be_visible()
    expect(page.get_by_role("combobox", name="State/Region")).to_be_visible()


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

    # Search for a park
    search_box = page.get_by_role("searchbox", name="Search parks")
    search_box.fill("Universal")
    search_box.press("Enter")

    # Verify search results
    expect(page.get_by_text("Universal Studios")).to_be_visible()
    expect(page.get_by_text("Universal's Islands of Adventure")).to_be_visible()


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

    # Select country filter
    page.get_by_role("combobox", name="Country").select_option("United States")

    # Select state filter
    page.get_by_role("combobox", name="State/Region").select_option("Florida")

    # Verify filtered results
    expect(page.get_by_text("Walt Disney World")).to_be_visible()
    expect(page.get_by_text("Universal Orlando Resort")).to_be_visible()


def test_park_detail_page(page: Page):
    # Navigate to a specific park page
    page.goto("http://localhost:8000/parks/walt-disney-world/")

    # Check park details
    expect(page.get_by_role("heading", name="Walt Disney World")).to_be_visible()
    expect(page.get_by_text("Location:")).to_be_visible()
    expect(page.get_by_text("Orlando, Florida")).to_be_visible()

    # Check park sections
    expect(page.get_by_role("tab", name="Overview")).to_be_visible()
    expect(page.get_by_role("tab", name="Rides")).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_add_park_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 park page
    page.goto("http://localhost:8000/parks/walt-disney-world/")

    # 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("Amazing Experience")
    page.get_by_label("Review").fill("Had a fantastic time at the park!")

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

    # Verify review appears
    expect(page.get_by_text("Amazing Experience")).to_be_visible()
    expect(page.get_by_text("Had a fantastic time at the park!")).to_be_visible()


def test_add_park_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 park page
    page.goto("http://localhost:8000/parks/walt-disney-world/")

    # 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("Beautiful castle at sunset")

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

    # Verify photo appears
    expect(page.get_by_text("Beautiful castle at sunset")).to_be_visible()


def test_park_map(page: Page):
    # Navigate to park page
    page.goto("http://localhost:8000/parks/walt-disney-world/")

    # Check map exists
    expect(page.locator("#park-map")).to_be_visible()

    # Check map controls
    expect(page.get_by_role("button", name="Zoom in")).to_be_visible()
    expect(page.get_by_role("button", name="Zoom out")).to_be_visible()

    # Verify map markers
    expect(page.locator(".map-marker")).to_have_count.greater_than(0)
