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.tests.订单流转.test_e2e_task_001_external_order_dispatch_to_player import login_player_sub_account_by_sms
from e2e.utils.account_store import E2EAccount
from e2e.utils.bug_reporter import BugReporter


def main() -> None:
    sys.stdout.reconfigure(encoding="utf-8")
    settings = settings_fixture.__wrapped__()
    source_order_no = os.getenv("E2E_DELIVERY_SOURCE_ORDER_NO", "E2EORDER0609120015")
    order_title = os.getenv("E2E_DELIVERY_ORDER_TITLE", f"E2E手工录单{source_order_no[-10:]}")
    player_phone = os.getenv("E2E_DELIVERY_PLAYER_PHONE", "19667441530")
    player_password = os.getenv("E2E_DELIVERY_PLAYER_PASSWORD", "test123456")

    player = E2EAccount(
        phone=player_phone,
        password=player_password,
        enterprise_name="",
        contact_name="",
        account_type="sub",
        parent_phone="",
        parent_enterprise_name="",
        member_role="打手",
        member_status="registered",
        member_invite_url="",
        certification_status="approved",
        wanyouji_status="opened",
        wanzhanggui_status="not_opened",
        wanzhanggui_binding_status="not_bound",
        bound_wanzhanggui_enterprise="",
        wanzhanggui_bindings=[],
        workshop_authorizations=[],
        dispatch_strategies=[],
        latest_auth_key="",
        merchant_id="",
        created_at="",
        updated_at="",
    )

    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-DELIVERY-001-PROBE")
        player_browser_page = login_player_sub_account_by_sms(page, settings, bug_reporter, player)
        delivery_page = WanyoujiFulfillmentPage(player_browser_page, settings.tenant_home_url, bug_reporter)
        delivery_page.submit_player_delivery_for_acceptance(source_order_no, order_title)
        print(f"打手交付上传探测通过：{source_order_no}")
        context.close()
        browser.close()


if __name__ == "__main__":
    main()
