from playwright.sync_api import sync_playwright
import json

# ── CONFIG ──────────────────────────────────────────────────────────────────
BASE_URL = "https://janssen.4gclinical.com"
STUDY    = "42847922MDD3003"

EMAIL    = "vbuzalka@its.jnj.com"
PASSWORD = "Vlado123++-"   # doplň heslo
# ────────────────────────────────────────────────────────────────────────────


def list_reports():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()

        # Přihlášení
        page.goto(BASE_URL)
        page.wait_for_load_state("networkidle")

        page.get_by_label("Email *").fill(EMAIL)
        page.get_by_label("Password *").fill(PASSWORD)
        page.locator('#login__submit').click()
        page.wait_for_load_state("networkidle")

        # Výběr studie — klikni na dropdown, vyber studii, klikni SELECT
        page.get_by_label("Study *").click()
        page.get_by_role("option", name=STUDY).click()
        page.get_by_role("button", name="SELECT").click()
        page.wait_for_load_state("networkidle")

        # Přejdi na seznam reportů
        page.goto(f"{BASE_URL}/reports")
        page.wait_for_load_state("networkidle")
        page.wait_for_selector('[role="gridcell"] a', timeout=15000)

        # Získej názvy reportů
        names = page.evaluate("""
            () => Array.from(document.querySelectorAll('[role="gridcell"] a'))
                .map(a => a.innerText.trim())
                .filter(n => n)
        """)
        print(f"\nNalezeno {len(names)} reportů, zjišťuji URL...\n")

        # Pro každý report klikni, zaznamenej URL a vrať se zpět
        reports = []
        for name in names:
            with page.expect_navigation(timeout=15000):
                page.locator('[role="gridcell"] a').filter(has_text=name).click()
            page.wait_for_load_state("networkidle")
            page.wait_for_timeout(2000)
            path = page.url.replace(BASE_URL, "")
            reports.append({"name": name, "href": path})
            print(f"  {name:50s}  {path}")
            # Průběžné uložení po každém reportu
            with open("reports.json", "w", encoding="utf-8") as f:
                json.dump(reports, f, ensure_ascii=False, indent=2)
            if page.url != f"{BASE_URL}/reports":
                page.goto(f"{BASE_URL}/reports")
            page.wait_for_load_state("networkidle")
            page.wait_for_timeout(2000)
            page.wait_for_selector('[role="gridcell"] a', timeout=30000)

        browser.close()

        with open("reports.json", "w", encoding="utf-8") as f:
            json.dump(reports, f, ensure_ascii=False, indent=2)
        print(f"\nUloženo do reports.json")

        return reports


list_reports()
