from __future__ import annotations

import os

import pytest
from playwright.sync_api import Page

from e2e.pages.wanzhanggui_order_page import WanyoujiFulfillmentPage
from e2e.tests.订单状态流转.penalty_recovery_flow_helpers import (
    resolve_order_for_case,
    resolve_wanyouji_account,
)
from e2e.utils.account_store import account_store


@pytest.mark.s_flow_031
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_031_continue_dispatch_after_player_revoke_approved(page: Page, settings, bug_reporter) -> None:
    wanzhanggui_phone = os.getenv("E2E_ORDER_WANZHANGGUI_PHONE", settings.wanzhanggui_tenant_phone)
    assignment_method = os.getenv("E2E_TASK_ASSIGNMENT_METHOD", "抢单大厅").strip()
    assert assignment_method in {"抢单大厅", "指派打手", "智能派单"}, (
        "E2E_TASK_ASSIGNMENT_METHOD 仅支持：抢单大厅、指派打手、智能派单；"
        f"当前值：{assignment_method}"
    )

    store = account_store()
    order = resolve_order_for_case(
        case_id="S-FLOW-031",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["player_sub_order_revoke_approved_terminal"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-031 使用的打手撤销完成订单；请先执行 S-FLOW-030。")

    source_order_no = order["source_order_no"]
    main_order_no = order.get("main_order_no", "")
    order_title = order.get("order_title", source_order_no)
    target_sub_order_no = order.get("task_player_sub_order_no") or order.get("player_revoke_approved_sub_order_no") or ""
    wanyouji_account = resolve_wanyouji_account(order)

    fulfillment_page = WanyoujiFulfillmentPage(page, settings.tenant_home_url, bug_reporter)
    fulfillment_page.login_main_account(wanyouji_account.phone, wanyouji_account.password)
    if not target_sub_order_no:
        target_sub_order_no, _ = fulfillment_page.find_workshop_sub_order_no_by_status(
            source_order_no,
            order_title,
            ["已撤销", "撤销完成", "撤销结束", "已取消", "取消订单"],
        )
    before_text, detail_text, after_text = fulfillment_page.continue_dispatch_cancelled_child_from_workshop_order_list(
        source_order_no,
        order_title,
        assignment_method=assignment_method,
        required_terminal_sub_order_no=target_sub_order_no,
    )
    assert any(status in after_text for status in ["待接单", "已发单", "代练中", "抢单大厅"]), (
        f"S-FLOW-031 二发新子单后未看到下一子单待接单/已发单状态；source_order_no={source_order_no}; "
        f"text={after_text[:1500]}"
    )
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-031 丸友集同意打手撤销后二发新子单：{main_order_no or source_order_no}/{assignment_method}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-031",
        status="task_dispatched_to_player_hall",
        current_flow_state="wanyouji_dispatched_to_player_hall",
        task_wanyouji_phone=wanyouji_account.phone,
        task_player_sub_order_no=target_sub_order_no,
        task_assignment_method=assignment_method,
        wanyouji_order_list_before_continue_dispatch_after_player_revoke_text=before_text[:1500],
        wanyouji_continue_dispatch_after_player_revoke_detail_text=detail_text[:1500],
        wanyouji_order_list_after_continue_dispatch_after_player_revoke_text=after_text[:1500],
        available_for_cases=["S-FLOW-026", "S-FLOW-075"],
    )
