from __future__ import annotations

import os

from dotenv import load_dotenv
from playwright.sync_api import sync_playwright

from probe_enterprise_auth_menu import ENTERPRISE_CONSOLE_URL, click_sidebar_row_right, click_sidebar_text
from probe_my_auth_activate_form import click_button, my_auth_frame
from probe_solution_console_entry import login_and_select_main


load_dotenv()


def main() -> None:
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False, slow_mo=120)
        context = browser.new_context(viewport={"width": 1440, "height": 900})
        page = context.new_page()
        login_and_select_main(
            page,
            os.getenv("E2E_WANZHANGGUI_TENANT_PHONE", "15555555555"),
            os.getenv("E2E_WANZHANGGUI_TENANT_PASSWORD", "wss123456"),
        )
        page.goto(ENTERPRISE_CONSOLE_URL, wait_until="domcontentloaded")
        page.wait_for_timeout(2500)
        click_sidebar_row_right(page, "企业")
        click_sidebar_text(page, "授权管理")
        click_sidebar_text(page, "我的授权")
        page.wait_for_timeout(2500)
        frame = my_auth_frame(page)
        if not click_button(frame, "激活密钥"):
            raise AssertionError("未点击到激活密钥")
        page.wait_for_timeout(1000)
        frame.locator(".ant-modal .ant-select-selector").first.click()
        page.wait_for_timeout(1000)
        options = frame.locator(".ant-select-dropdown:not(.ant-select-dropdown-hidden) .ant-select-item-option-content")
        values = [options.nth(i).inner_text(timeout=1000).strip() for i in range(options.count())]
        print(values)
        browser.close()


if __name__ == "__main__":
    main()
