from tests.conftest import BASE_URL


def test_bugs_page_loads(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    assert page.is_visible("h1:has-text('Bug Reports')")


def test_bugs_create_modal(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    page.click("button:has-text('Report Bug')")
    assert page.is_visible("h3:has-text('Report a Bug')")


def test_bugs_empty_state(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    assert page.is_visible("text=No bug reports yet.")


def test_bugs_create_submit(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    assert page.is_visible("text=No bug reports yet.")


def test_bugs_footer_link(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
    link = page.locator("a:has-text('Bug Report')")
    assert link.is_visible()
    link.click()
    page.wait_for_url(f"{BASE_URL}/bugs", wait_until="domcontentloaded")


def test_bugs_create_cancel(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    page.click("button:has-text('Report Bug')")
    page.click("button:has-text('Cancel')")
    modal = page.locator("#create-bug-modal")
    assert not modal.is_visible()


def test_bugs_unauth_empty(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    assert page.is_visible("h1:has-text('Bug Reports')")


def test_bug_comments_form_visible(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    page.click("button:has-text('Report Bug')")
    page.fill("#bug-title", "Bug with comments")
    page.fill("#bug-description", "Testing comment form on bug")
    page.click("button:has-text('Submit Report')")
    page.wait_for_timeout(500)
    assert page.is_visible("text=Comments")
    assert page.is_visible("textarea[name='content']")


def test_bug_comment_create(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    page.click("button:has-text('Report Bug')")
    page.fill("#bug-title", "Bug comment test")
    page.fill("#bug-description", "Bug description for comment")
    page.click("button:has-text('Submit Report')")
    page.wait_for_timeout(500)
    page.fill("textarea[name='content']", "Bug comment here")
    page.click("button:has-text('Post')")
    page.wait_for_timeout(500)
    assert page.is_visible("text=Bug comment here")


def test_bug_comment_reply(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    page.click("button:has-text('Report Bug')")
    page.fill("#bug-title", "Bug threaded reply")
    page.fill("#bug-description", "Bug for thread test")
    page.click("button:has-text('Submit Report')")
    page.wait_for_timeout(500)
    page.fill("textarea[name='content']", "Root comment")
    page.click("button:has-text('Post')")
    page.wait_for_timeout(500)
    assert page.is_visible("text=Root comment")
    page.click("button:has-text('Reply')")
    page.fill("textarea[name='content']", "Nested reply")
    page.click("button:has-text('Post')")
    page.wait_for_timeout(500)
    assert page.is_visible("text=Nested reply")


def test_bug_comment_delete(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    page.click("button:has-text('Report Bug')")
    page.fill("#bug-title", "Bug delete comment")
    page.fill("#bug-description", "Bug for delete test")
    page.click("button:has-text('Submit Report')")
    page.wait_for_timeout(500)
    page.fill("textarea[name='content']", "Delete me")
    page.click("button:has-text('Post')")
    page.wait_for_timeout(500)
    assert page.is_visible("text=Delete me")
    page.locator(".comment-body:has-text('Delete me') .comment-action-btn:has-text('Delete')").click()
    page.wait_for_timeout(500)
    assert not page.is_visible("text=Delete me")


def test_bug_button_icon_spacing(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/bugs", wait_until="domcontentloaded")
    html = page.locator("button:has-text('Report Bug')").inner_html()
    assert "</span> Report Bug" in html
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    landing = page.locator("a:has-text('Bug Report')").inner_html()
    assert "</span> Bug Report" in landing
    page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
    footer = page.locator("a:has-text('Bug Report')").inner_html()
    assert "</span> Bug Report" in footer
