# Název:  download_kit_inventory_v2.1.py
# Verze:  2.1
# Datum:  2026-05-28
# Popis:  Stahuje kit inventory (on-hand expiration) ze Covance/Labcorp XSP
#         pro 2 studie: 77242113UCO3001 (study 36940) a study 35472.
#         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/"
OUT_DIR    = r"U:\PythonProject\Janssen\Covance\Source"
PROFILE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "browser_profile")

REPORTS = [
    {
        "study": "77242113UCO3001",
        "url": (
            "https://xsp.labcorp.com/sponsor/study/36940/kit-inventory/on-hand-expiration"
            "?site=%5B%22CZ10001-213069%22,%22CZ10010-213069%22,%22CZ10022-213069%22,"
            "%22CZ10020-213069%22,%22CZ10012-213069%22,%22CZ10021-213069%22,"
            "%22CZ10016-213069%22,%22CZ10003-213069%22,%22CZ10009-213069%22,"
            "%22CZ10015-213069%22,%22CZ10013-213069%22,%22CZ10006-213069%22%5D"
            "&recordLimit=true"
        ),
        "filename": "sponsor-study-36940-kit-inventory-on-hand-expiration.csv",
    },
    {
        "study": "druha studie (35472)",
        "url": (
            "https://xsp.labcorp.com/sponsor/study/35472/kit-inventory/on-hand-expiration"
            "?site=%5B%22CZ10004-212396%22,%22CZ10012-212396%22,"
            "%22CZ10011-212396%22,%22CZ10008-212396%22%5D"
            "&recordLimit=true"
        ),
        "filename": "sponsor-study-35472-kit-inventory-on-hand-expiration.csv",
    },
]


def login(page):
    page.goto(LOGIN_URL)
    page.wait_for_load_state("networkidle")
    if not page.get_by_label("Email").is_visible():
        print(f"Session aktivni, prihlasen: {page.url}")
        return
    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 download_report(page, report):
    print(f"\n--- {report['study']} ---")
    page.goto(report["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()

    timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")
    dest = os.path.join(OUT_DIR, f"{timestamp} {report['filename']}")
    dl.value.save_as(dest)
    print(f"Stazeno: {dest}")


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)
        for report in REPORTS:
            download_report(page, report)
        context.close()
