from playwright.sync_api import Page, expect


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

    # Check reviews list elements
    expect(page.get_by_role("heading", name="Latest Reviews")).to_be_visible()
    expect(page.get_by_role("searchbox", name="Search reviews")).to_be_visible()

    # Check filter options
    expect(page.get_by_role("combobox", name="Type")).to_be_visible()
    expect(page.get_by_role("combobox", name="Rating")).to_be_visible()
    expect(page.get_by_role("combobox", name="Sort By")).to_be_visible()


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

    # Search for reviews
    search_box = page.get_by_role("searchbox", name="Search reviews")
    search_box.fill("great experience")
    search_box.press("Enter")

    # Verify search results
    expect(page.get_by_text("great experience", exact=False)).to_be_visible()


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

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

    # Select rating filter
    page.get_by_role("combobox", name="Rating").select_option("5 Stars")

    # Select sort order
    page.get_by_role("combobox", name="Sort By").select_option("Most Recent")

    # Verify filtered results
    expect(page.get_by_text("Park Review")).to_be_visible()
    expect(page.locator(".five-star-rating")).to_be_visible()


def test_review_detail_page(page: Page):
    # Navigate to a specific review
    page.goto("http://localhost:8000/reviews/1/")

    # Check review details
    expect(page.get_by_role("heading", name="Review")).to_be_visible()
    expect(page.locator(".review-rating")).to_be_visible()
    expect(page.locator(".review-date")).to_be_visible()
    expect(page.locator(".review-author")).to_be_visible()
    expect(page.locator(".review-content")).to_be_visible()


def test_review_voting(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 review
    page.goto("http://localhost:8000/reviews/1/")

    # Click helpful button
    page.get_by_role("button", name="Helpful").click()

    # Verify vote registered
    expect(page.get_by_text("You found this review helpful")).to_be_visible()
    expect(page.locator(".helpful-count")).to_contain_text("1")


def test_review_comments(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 review
    page.goto("http://localhost:8000/reviews/1/")

    # Add comment
    page.get_by_label("Comment").fill("Great review! Very helpful.")
    page.get_by_role("button", name="Post Comment").click()

    # Verify comment appears
    expect(page.get_by_text("Great review! Very helpful.")).to_be_visible()


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

    # Navigate to review
    page.goto("http://localhost:8000/reviews/1/")

    # Check moderation options
    expect(page.get_by_role("button", name="Edit Review")).to_be_visible()
    expect(page.get_by_role("button", name="Delete Review")).to_be_visible()
    expect(page.get_by_role("button", name="Flag Review")).to_be_visible()


def test_review_reporting(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 review
    page.goto("http://localhost:8000/reviews/1/")

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

    # Fill report form
    page.get_by_label("Reason").select_option("inappropriate")
    page.get_by_label("Details").fill("This review contains inappropriate content")

    # Submit report
    page.get_by_role("button", name="Submit Report").click()

    # Verify report confirmation
    expect(page.get_by_text("Review reported successfully")).to_be_visible()


def test_review_editing(page: Page):
    # First login as review author
    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 own review
    page.goto("http://localhost:8000/reviews/1/")

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

    # Modify review
    page.get_by_label("Title").fill("Updated Review Title")
    page.get_by_label("Review").fill("Updated review content")

    # Save changes
    page.get_by_role("button", name="Save Changes").click()

    # Verify updates
    expect(page.get_by_text("Updated Review Title")).to_be_visible()
    expect(page.get_by_text("Updated review content")).to_be_visible()
    expect(page.get_by_text("(edited)")).to_be_visible()
