# retoor <retoor@molodetz.nl>

from tests.conftest import BASE_URL


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


def test_issues_filters_visible(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/issues", wait_until="domcontentloaded")
    assert page.locator(".issues-filter:has-text('Open')").is_visible()
    assert page.locator(".issues-filter:has-text('Closed')").is_visible()
    assert page.locator(".issues-filter:has-text('All')").is_visible()


def test_issues_not_configured_notice(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/issues", wait_until="domcontentloaded")
    assert page.is_visible("text=The issue tracker is not configured yet.")


def test_issues_create_modal(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/issues", wait_until="domcontentloaded")
    page.click("button:has-text('Report Issue')")
    assert page.is_visible("h3:has-text('Report an Issue')")


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


def test_issues_unauth(page):
    page.goto(f"{BASE_URL}/issues", wait_until="domcontentloaded")
    assert page.is_visible("h1:has-text('Issue Reports')")
    assert page.locator("a.login-required:has-text('Report Issue')").is_visible()
    assert page.locator("#create-issue-modal").count() == 0


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


def test_issue_detail_not_configured(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/issues/999", wait_until="domcontentloaded")
    assert page.is_visible("h1.error-code:has-text('404')")
    assert page.is_visible("text=Page not found")
