# Název:  download_equeries_report_v1.1.py
# Verze:  1.1
# Datum:  2026-05-28
# Popis:  Stahuje eQuery reporty ze Covance/Labcorp XSP pro studii 36940.
#         Stahuje 2 soubory: všechny eQueries + pouze nezodpovězené.
#         Výstup: timestampované CSV soubory do adresáře Source/.
#         Prohlížeč spouštěn maximalizovaný.

from playwright.sync_api import sync_playwright
from datetime import datetime
import os

EMAIL      = "vbuzalka@its.jnj.com"
PASSWORD   = "%zT3Wqfc9)cWua5"
LOGIN_URL  = "https://xsp.covance.com/"
SITES = "%5B%22930551%22,%22930556%22,%22930525%22,%22930549%22,%22930543%22,%22930547%22,%22930555%22,%22930557%22,%22930539%22,%22930536%22,%22930553%22,%22930531%22%5D"
REPORT_URL             = f"https://xsp.labcorp.com/sponsor/study/36940/activity-reports/documents/equery?site={SITES}"
REPORT_URL_UNRESPONDED = f"https://xsp.labcorp.com/sponsor/study/36940/activity-reports/documents/equery?site={SITES}&unrespondedOnly=true"
OUT_DIR     = r"U:\PythonProject\Janssen\Covance_UCO3001\Source"
PROFILE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "browser_profile")


def login(page):
    page.goto(LOGIN_URL)
    page.wait_for_load_state("networkidle")
    page.get_by_label("Email").fill(EMAIL)
    page.get_by_role("button", name="Next").click()
    page.wait_for_load_state("networkidle")
    page.get_by_label("Password").fill(PASSWORD)
    page.get_by_role("button", name="Verify").click()
    page.wait_for_url(lambda url: "code=" not in url, timeout=60000)
    page.wait_for_load_state("networkidle", timeout=60000)
    page.wait_for_timeout(2000)
    print(f"Prihlaseni OK: {page.url}")


def export_csv(page, url, filename):
    page.goto(url)
    page.wait_for_load_state("networkidle", timeout=60000)
    print(f"Report nacteny: {page.url}")
    page.locator("ag-export").get_by_role("button", name="more_horiz").click()
    with page.expect_download(timeout=60000) as dl:
        page.get_by_text("Export to CSV").click()
    dest = os.path.join(OUT_DIR, filename)
    dl.value.save_as(dest)
    print(f"Stazeno: {dest}")


def download(page):
    timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")
    export_csv(page, REPORT_URL,
               f"{timestamp} sponsor-study-36940-activity-reports-documents-equery.csv")
    export_csv(page, REPORT_URL_UNRESPONDED,
               f"{timestamp} sponsor-study-36940-activity-reports-documents-equery_unresponded_only.csv")


if __name__ == "__main__":
    with sync_playwright() as p:
        context = p.chromium.launch_persistent_context(
            user_data_dir=PROFILE_DIR,
            headless=False,
            args=["--disable-blink-features=AutomationControlled", "--start-maximized"],
            no_viewport=True,
            user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36",
            accept_downloads=True,
        )
        context.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
        page = context.new_page()
        login(page)
        download(page)
        context.close()
