from __future__ import annotations

import os
import sys

from playwright.sync_api import sync_playwright

sys.path.insert(0, os.getcwd())

from e2e.conftest import settings as settings_fixture
from e2e.pages.wanzhanggui_order_page import WanyoujiFulfillmentPage
from e2e.utils.bug_reporter import BugReporter


def main() -> None:
    sys.stdout.reconfigure(encoding="utf-8")
    settings = settings_fixture.__wrapped__()
    phone = os.getenv("E2E_TASK_WANYOUJI_PHONE", "19844527868")
    password = os.getenv("E2E_TASK_WANYOUJI_PASSWORD", "test123456")

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        context = browser.new_context(viewport={"width": settings.viewport_width, "height": settings.viewport_height})
        page = context.new_page()
        bug_reporter = BugReporter(page, case_id="E2E-WORKSHOP-ORDER-LIST-PROBE")
        owner_page = WanyoujiFulfillmentPage(page, settings.tenant_home_url, bug_reporter)
        print("开始登录丸友集主账号")
        owner_page.login_main_account(phone, password)
        print(f"登录后页面：{page.url}")
        print("开始打开工作室履约应用-订单列表")
        frame = owner_page.open_workshop_order_list()
        print(f"订单列表 frame：{frame.url}")
        text = frame.locator("body").inner_text(timeout=5000)
        assert owner_page.workshop_order_list_is_ready(text), f"未进入订单列表；text={text[:1200]}"
        print("已稳定进入工作室履约应用-订单列表")
        context.close()
        browser.close()


if __name__ == "__main__":
    main()
