"""Test: the jobs screen has a discreet "?" info button that opens a one-page
feature guide, which closes on backdrop tap or the "Got it" button."""
import os
import sys
import time

sys.stdout.reconfigure(encoding="utf-8", errors="replace")

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

HERE = os.path.dirname(os.path.abspath(__file__))
GECKODRIVER = os.path.join(HERE, "geckodriver.exe")
BASE = "http://localhost:8000/field-capture.html?debug"


def main():
    opts = Options()
    opts.headless = False
    svc = Service(executable_path=GECKODRIVER)
    d = webdriver.Firefox(options=opts, service=svc)
    try:
        d.get(BASE)
        time.sleep(2)

        # 1. The discreet "?" info button is present on the jobs screen.
        info_btn = d.find_elements(By.CSS_SELECTOR, '.topbar [data-action="info"]')
        print("info button present:", len(info_btn) > 0)
        print("info button text:", info_btn[0].text if info_btn else "(none)")

        # 2. Info sheet is NOT shown before tapping.
        sheet_before = d.find_elements(By.CSS_SELECTOR, ".sheet-overlay")
        print("sheet before tap:", len(sheet_before) > 0)

        # 3. Tap the info button -> sheet appears with feature items.
        info_btn[0].click()
        time.sleep(0.6)
        overlay = d.find_elements(By.CSS_SELECTOR, ".sheet-overlay")
        print("sheet after tap:", len(overlay) > 0)
        items = d.find_elements(By.CSS_SELECTOR, ".info-item")
        print("info item count:", len(items))
        # Spot-check a couple of the documented features.
        texts = [el.text for el in items]
        joined = "\n".join(texts)
        print("has 'Copy a line':", "Copy a line" in joined)
        print("has 'Move one endpoint':", "Move one endpoint" in joined)
        print("has 'Auto numbering':", "Auto numbering" in joined)

        # 4. "Got it" button closes the sheet.
        got_it = d.find_elements(By.CSS_SELECTOR, '.sheet [data-action="closeinfo"]')
        print("got-it button present:", len(got_it) > 0)
        got_it[0].click()
        time.sleep(0.6)
        overlay_after = d.find_elements(By.CSS_SELECTOR, ".sheet-overlay")
        print("sheet after close:", len(overlay_after) > 0)

        # 5. Reopen and close via backdrop tap.
        d.find_element(By.CSS_SELECTOR, '.topbar [data-action="info"]').click()
        time.sleep(0.6)
        print("reopened:", len(d.find_elements(By.CSS_SELECTOR, ".sheet-overlay")) > 0)
        # Click the overlay backdrop directly (target === overlay).
        d.execute_script("""
            const ov = document.querySelector('.sheet-overlay');
            ov.dispatchEvent(new MouseEvent('click', { bubbles: true }));
        """)
        time.sleep(0.6)
        print("closed via backdrop:", len(d.find_elements(By.CSS_SELECTOR, ".sheet-overlay")) == 0)
    finally:
        d.quit()


if __name__ == "__main__":
    main()
