#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
from playwright.sync_api import sync_playwright

STATE_FILE = "../medevio_storage.json"
REQUEST_URL = (
    "https://my.medevio.cz/mudr-buzalkova/klinika/"
    "pozadavky?pozadavek=e28cbf71-8280-4078-a881-c44119bbccc2"
)

def main():
    with sync_playwright() as pw:
        browser = pw.chromium.launch(headless=False)
        context = browser.new_context(storage_state=STATE_FILE)
        page = context.new_page()

        # otevři konkrétní požadavek
        page.goto(REQUEST_URL, wait_until="domcontentloaded", timeout=60_000)

        # combobox Fronta
        combo = page.locator('div[role="combobox"][aria-labelledby="queue-select-label"]')
        combo.wait_for(state="visible")
        combo.click()

        # vyber „MUDr. Buzalka (já)“
        option = page.get_by_role("option", name=re.compile(r"MUDr\.?\s*Buzalka", re.I))
        option.click()

        # počkej, dokud síť neutichne (změna je odeslaná/uložená)
        page.wait_for_load_state("networkidle")

        # zavři dialog
        page.locator("button.MuiDialog-close").click()

        context.close()
        browser.close()

if __name__ == "__main__":
    main()
