from playwright.sync_api import sync_playwright
import os

AUTH_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "auth.json")
EMAIL     = "vbuzalka@its.jnj.com"
PASSWORD  = "%zT3Wqfc9)cWua5"
URL       = "https://xsp.covance.com/"


if __name__ == "__main__":
    with sync_playwright() as p:
        browser = p.chromium.launch(
            headless=False,
            args=["--disable-blink-features=AutomationControlled"],
        )
        context = browser.new_context(
            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"
        )
        context.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
        page = context.new_page()

        page.goto(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()
        print("Cekam na dokonceni prihlaseni...")
        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"Dashboard URL: {page.url}")
        print("Naviguj na stranku s kit inventory reportem,")
        print("pak stiskni Enter pro ulozeni session...")
        input()

        context.storage_state(path=AUTH_FILE)
        print(f"Session ulozena: {AUTH_FILE}")
        print(f"Aktualni URL: {page.url}")
        browser.close()
