# retoor <retoor@molodetz.nl>

from tests.conftest import BASE_URL
from devplacepy.database import get_table, refresh_snapshot


def _clear_seo_jobs():
    refresh_snapshot()
    jobs = get_table("jobs")
    for row in list(jobs.find(kind="seo")):
        jobs.delete(uid=row["uid"])


def test_seo_page_loads(page, app_server):
    page.goto(f"{BASE_URL}/tools/seo", wait_until="domcontentloaded")
    page.locator("[data-seo-tool]").wait_for(state="visible")
    page.locator("[data-seo-form] input[name='url']").wait_for(state="visible")
    page.locator("[data-seo-run]").wait_for(state="visible")


def test_seo_page_mode_toggle_reveals_pages(page, app_server):
    page.goto(f"{BASE_URL}/tools/seo", wait_until="domcontentloaded")
    page.locator("[data-seo-form]").wait_for(state="visible")
    pages_label = page.locator("[data-seo-pages]")
    assert pages_label.is_hidden()
    page.locator("[data-seo-mode]").select_option("sitemap")
    pages_label.wait_for(state="visible")


def test_seo_form_submit_shows_live_section(alice):
    page, _user = alice
    try:
        page.goto(f"{BASE_URL}/tools/seo", wait_until="domcontentloaded")
        page.locator("[data-seo-form]").wait_for(state="visible")
        page.locator("[data-seo-form] input[name='url']").fill("https://example.com")
        page.locator("[data-seo-run]").click()
        page.locator("[data-seo-live]").wait_for(state="visible")
    finally:
        _clear_seo_jobs()
