from pathlib import Path

import pytest


sync_api = pytest.importorskip("playwright.sync_api")

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


@pytest.mark.parametrize("width,height", [(320, 568), (390, 844)])
def test_search_week_plan_sheet_fits_small_phones_with_touch_safe_controls(width, height):
    html = (FRONTEND / "index.html").read_text()
    with sync_api.sync_playwright() as playwright:
        try:
            browser = playwright.chromium.launch(headless=True)
        except Exception as error:
            pytest.skip(f"Chromium unavailable: {error}")
        page = browser.new_page(viewport={"width": width, "height": height})
        page.set_content(html)
        page.add_style_tag(path=FRONTEND / "dashboard.css")
        page.locator("#search-week-plan-days").evaluate(
            """node => node.innerHTML = Array.from({length:7}, (_, index) =>
              `<button type="button" data-search-week-date="2026-08-${21+index}" aria-pressed="${index===0}">
                <strong>Day ${index+1}</strong><small>${index*30} of 120 min · ${index} planned</small>
              </button>`).join('')"""
        )
        page.locator("#search-week-plan-sheet").evaluate("node => node.hidden = false")
        page.locator("#search-week-plan-estimate").fill("45")

        expect = sync_api.expect
        expect(page.locator("#search-week-plan-sheet")).to_be_visible()
        expect(page.locator("#confirm-search-week-plan")).to_be_visible()
        for button in page.locator("#search-week-plan-days button").all():
            bounds = button.bounding_box()
            assert bounds and bounds["height"] >= 44
        assert page.locator("#confirm-search-week-plan").bounding_box()["height"] >= 44
        assert page.locator("#cancel-search-week-plan").bounding_box()["height"] >= 44
        assert page.evaluate("document.documentElement.scrollWidth <= window.innerWidth")
        assert page.locator(".search-week-plan-panel").evaluate(
            "node => node.scrollWidth <= node.clientWidth"
        )
        browser.close()


@pytest.mark.parametrize("width,height", [(320, 568), (390, 844)])
def test_search_week_batch_review_fits_small_phones_without_horizontal_overflow(width, height):
    html = (FRONTEND / "index.html").read_text()
    with sync_api.sync_playwright() as playwright:
        try:
            browser = playwright.chromium.launch(headless=True)
        except Exception as error:
            pytest.skip(f"Chromium unavailable: {error}")
        page = browser.new_page(viewport={"width": width, "height": height})
        page.set_content(html)
        page.add_style_tag(path=FRONTEND / "dashboard.css")
        page.locator("#search-week-batch-list").evaluate(
            """node => node.innerHTML = Array.from({length:4}, (_, index) =>
              `<div class="search-week-batch-row"><div><span class="small">stackchain/dashboard#${1196+index}</span>
                <strong>Substantial mobile planning issue ${index+1}</strong></div>
                <div class="search-week-batch-row-controls"><label>Day<select><option>Fri, Aug 21 · 2 planned</option></select></label>
                <label>Minutes<input type="number" value="45"></label></div></div>`).join('')"""
        )
        page.locator("#search-week-batch-review").evaluate("node => node.hidden = false")
        page.locator("#cmd-palette").evaluate("node => node.classList.add('open')")

        expect = sync_api.expect
        expect(page.locator("#search-week-batch-review")).to_be_visible()
        expect(page.locator("#confirm-search-week-batch")).to_be_visible()
        for control in page.locator("#search-week-batch-review select, #search-week-batch-review input, #search-week-batch-review button").all():
            bounds = control.bounding_box()
            assert bounds and bounds["height"] >= 44
        assert page.evaluate("document.documentElement.scrollWidth <= window.innerWidth")
        assert page.locator("#search-week-batch-review").evaluate(
            "node => node.scrollWidth <= node.clientWidth"
        )
        browser.close()
