"""Frontend E2E tests — GPS badge states."""

import pytest


def _login(page):
    """Helper: login as admin."""
    page.locator("#loginUsername").fill("admin")
    page.locator("#loginPassword").fill("changeme")
    page.locator("button:has-text('Sign In')").click()
    page.wait_for_selector("#loginOverlay", state="hidden", timeout=5000)


@pytest.mark.frontend
def test_gps_badge_shows_ok_when_geolocation_granted(page, live_server):
    """With geolocation permission granted, GPS badge shows OK state."""
    _login(page)

    # Wait for GPS to initialize (initGPS() runs on page load,
    # and with permissions=['geolocation'] set in browser context,
    # navigator.geolocation.getCurrentPosition succeeds immediately)
    gps_badge = page.locator("#gpsBadge")
    gps_badge.wait_for(timeout=10000)

    # The badge should exist and show coordinates (OK state)
    badge_text = gps_badge.inner_text()
    assert "📍" in badge_text
    # Check it's in OK state (class contains 'ok')
    assert "ok" in gps_badge.get_attribute("class")
