"""Home navigation helpers for OfficeSite."""
from __future__ import annotations

from playwright.sync_api import Page

from ..selectors import officesite as officesite_sel


def ensure_home(page: Page) -> None:
    """
    Ensure we're on OfficeSite Home by clicking the Home menu if present.
    Falls back to direct OfficeSite URL.
    """
    try:
        # Direct selectors seen in top nav
        candidates = [
            page.locator("[data-aroflo-menuitem='Home']"),
            page.locator(".menuBtn.type-home"),
            page.get_by_role("link", name="Home"),
            page.get_by_role("button", name="Home"),
            page.locator("a:has-text('Home')"),
            page.locator("button:has-text('Home')"),
        ]
        for loc in candidates:
            try:
                if loc.count() == 0:
                    continue
                target = loc.first if hasattr(loc, "first") else loc
                try:
                    target.hover(timeout=1500)
                except Exception:
                    pass
                target.click(timeout=3000, force=True)
                page.wait_for_timeout(350)
                return
            except Exception:
                continue
    except Exception:
        pass

    # Fallback: direct URL
    officesite_sel.ensure_officesite(page, force=True)
