from pathlib import Path

from src.frontend_bundle import build_frontend


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


def test_search_preview_exposes_accessible_plan_ahead_sheet_and_packaged_controller():
    html = (FRONTEND / "index.html").read_text()
    build = build_frontend(FRONTEND)

    assert 'id="plan-search-result"' in html
    assert 'id="search-week-plan-sheet" role="dialog" aria-modal="true"' in html
    assert 'aria-labelledby="search-week-plan-title"' in html
    assert 'id="search-week-plan-days"' in html
    assert 'id="search-week-plan-estimate"' in html
    assert 'id="confirm-search-week-plan"' in html
    assert 'id="cancel-search-week-plan"' in html
    assert "static/search-week-plan.js" in build.page_sources
    assert b"createSearchWeekPlan" in build.feature_bundles["planning"].runtime_bytes


def test_search_plan_ahead_ui_connects_eligibility_preview_and_truthful_outcomes():
    dashboard = (FRONTEND / "dashboard.js").read_text()
    source = (FRONTEND / "search-week-plan.js").read_text()

    assert "const searchWeekPlan = createSearchWeekPlan" in dashboard
    assert "createSearchWeekPlanUI" in dashboard
    assert "planButton.hidden = !searchWeekPlan.eligible(detail)" in dashboard
    assert "await planner.preview(detail)" in source
    assert "overload-confirmation-required" in source
    assert "assigned-not-planned" in source
    assert "planned-pending" in source
    assert "Already planned for" in source
    assert "planner.pending()" in source
    assert "searchWeekPlan:true" in source
    assert "if(!qs('#search-week-plan-sheet').hidden)" in source
    assert "window.history.back()" in source


def test_search_plan_ahead_sheet_is_phone_safe_and_touch_sized():
    css = (FRONTEND / "dashboard.css").read_text()

    assert ".search-week-plan-sheet" in css
    assert "padding-bottom:calc(18px + env(safe-area-inset-bottom))" in css
    assert ".search-week-plan-days button" in css
    assert "min-height:44px" in css
    assert "overflow-x:hidden" in css
    assert "@media (max-width:359px)" in css
