# Test: najit posledni odeslany email na klucho@gastroenterolog.com,
# preposlat na vladimir.buzalka@buzalka.cz, predmet "Ahoj", prvni radek "Ahoj"

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    context = browser.new_context(storage_state="outlook_auth.json")
    page = context.new_page()

    # 1. Otevrit Outlook
    page.goto("https://outlook.cloud.microsoft/mail/")
    page.wait_for_selector('[placeholder="Search or ask Copilot"]')

    # 2. Prejit do Sent Items
    page.click('text=Sent Items')
    page.wait_for_url("**/sentitems")

    # 3. Vyhledat emaily na klucho@gastroenterolog.com
    search = page.locator('[placeholder="Search or ask Copilot"]')
    search.click()
    search.fill("to:klucho@gastroenterolog.com")
    search.press("Enter")
    page.wait_for_selector("text=All results")
    page.wait_for_timeout(1000)

    # 4. Kliknout na prvni (nejnovejsi) email
    page.locator('[role="option"]').first.click()
    page.wait_for_selector('button:has-text("Forward"), [aria-label="Forward"]')

    # 5. Kliknout na Forward
    page.locator('button[aria-label="Forward"]').first.click()
    page.wait_for_selector('[aria-label="To"]', timeout=5000)

    # 6. Vyplnit prijemce
    page.locator('[aria-label="To"]').fill("vladimir.buzalka@buzalka.cz")
    page.keyboard.press("Tab")

    # 7. Zmenit predmet na "Ahoj"
    subject = page.locator('[aria-label="Subject"]')
    subject.triple_click()
    subject.type("Ahoj")

    # 8. Napsat "Ahoj" na prvni radek tela emailu
    body = page.locator('[aria-label="Message body"]')
    body.click()
    page.keyboard.press("Control+Home")
    page.keyboard.type("Ahoj")
    page.keyboard.press("Enter")

    # 9. Odeslat
    page.click('button[aria-label="Send"]')
    page.wait_for_timeout(2000)

    print("Email uspesne odeslan!")
    browser.close()
