from __future__ import annotations

import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from e2e.pages.wanzhanggui_order_page import WanzhangguiOrderPage
from e2e.conftest import settings as settings_fixture
from e2e.utils.bug_reporter import BugReporter
from playwright.sync_api import sync_playwright


MAIN_ORDER_NO = "BO20260624142123388152"


def main() -> None:
    settings = settings_fixture.__wrapped__()
    source_order_no = os.getenv("E2E_S_FLOW_SOURCE_ORDER_NO", "E2EORDER0624142043")
    order_title = os.getenv("E2E_S_FLOW_ORDER_TITLE", "E2E手工录单已推单0624142043")
    phone = os.getenv("E2E_ORDER_WANZHANGGUI_PHONE", settings.wanzhanggui_tenant_phone)
    password = os.getenv("E2E_ORDER_WANZHANGGUI_PASSWORD", settings.wanzhanggui_tenant_password)

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False, slow_mo=100)
        page = browser.new_page(viewport={"width": settings.viewport_width, "height": settings.viewport_height})
        reporter = BugReporter(page, case_id="S-FLOW-016")
        order_page = WanzhangguiOrderPage(page, settings.tenant_home_url, reporter)
        order_page.login_main_account(phone, password)
        detail_text = order_page.assert_order_detail_statuses(
            main_order_no=MAIN_ORDER_NO,
            source_order_no=source_order_no,
            order_title=order_title,
            expected_main_status=["待接单", "异常中"],
            expected_sub_status=["代练中异常", "待接单-已推单", "撤销结束"],
            expected_sub_order_list_status="撤销结束",
            expected_latest_sub_order_list_status="撤销结束",
            expected_aftersale_absent=True,
        )
        sub_order_text = order_page.detail_section_text(detail_text, "子单列表")
        print("S-FLOW-016 expected status verified")
        print("main_order_no=", MAIN_ORDER_NO)
        print("source_order_no=", source_order_no)
        print("has_revoke_end=", "撤销结束" in sub_order_text)
        print("has_continue_dispatch=", "继续发单" in detail_text)
        print("sub_order_list=")
        print(sub_order_text[:2000])
        browser.close()


if __name__ == "__main__":
    main()
