import os
from pathlib import Path

import pytest

if os.getenv("STACKCHAIN_RUN_RELEASE_E2E") != "1":
    pytest.skip("packaged mobile detail-watch journey runs only in its gated CI job", allow_module_level=True)
pytest.importorskip("playwright.sync_api")
from playwright.sync_api import expect, sync_playwright


ROOT = Path(__file__).parents[2]
FRONTEND = ROOT / "frontend"


@pytest.mark.parametrize("viewport", [
    {"width": 320, "height": 568},
    {"width": 390, "height": 844},
])
def test_issue_and_pull_detail_watch_controls_are_phone_usable(viewport):
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=True)
        page = browser.new_page(viewport=viewport)
        page.set_content((FRONTEND / "index.html").read_text())
        page.add_style_tag(path=FRONTEND / "dashboard.css")
        page.add_script_tag(path=FRONTEND / "search-preview.js")

        for sheet, button, expected in [
            ("#issue-sheet", "#watch-issue-detail", "Watch issue"),
            ("#pull-sheet", "#watch-pull-detail", "Watch pull request"),
        ]:
            page.locator(sheet).evaluate("node => node.classList.add('open')")
            control = page.locator(button)
            expect(control).to_be_visible()
            expect(control).to_have_text(expected)
            assert control.bounding_box()["height"] >= 44
            control.focus()
            expect(control).to_be_focused()
            page.locator(sheet).evaluate("node => node.classList.remove('open')")

        assert page.evaluate(
            "document.documentElement.scrollWidth > document.documentElement.clientWidth"
        ) is False
        browser.close()
