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.订单流转.test_e2e_task_001_external_order_dispatch_to_player import login_player_sub_account_by_sms
from e2e.tests.订单状态流转.penalty_recovery_flow_helpers import (
    resolve_order_for_case,
    resolve_player_account,
    resolve_wanyouji_account,
)
from e2e.utils.account_store import account_store


@pytest.mark.s_flow_029
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_029_wanyouji_reject_player_revoke_continue_training(page: Page, settings, bug_reporter) -> None:
    wanzhanggui_phone = os.getenv("E2E_ORDER_WANZHANGGUI_PHONE", settings.wanzhanggui_tenant_phone)
    store = account_store()
    order = resolve_order_for_case(
        case_id="S-FLOW-029",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["player_revoke_pending_at_wanyouji"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-029 使用的打手撤销中订单；请先执行 S-FLOW-027。")

    source_order_no = order["source_order_no"]
    main_order_no = order.get("main_order_no", "")
    order_title = order.get("order_title", source_order_no)
    remark = os.getenv("E2E_S_FLOW_PLAYER_REVOKE_REJECT_REMARK", f"S-FLOW-029 丸友集拒绝打手撤销 {main_order_no or source_order_no}")
    wanyouji_account = resolve_wanyouji_account(order)
    player_account = resolve_player_account(order, wanyouji_account)

    fulfillment_page = WanyoujiFulfillmentPage(page, settings.tenant_home_url, bug_reporter)
    fulfillment_page.login_main_account(wanyouji_account.phone, wanyouji_account.password)
    reject_text = fulfillment_page.reject_player_revoke_from_workshop_order(source_order_no, order_title, remark)
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-029 丸友集拒绝打手撤销：{main_order_no or source_order_no}",
    )
    assert any(key in reject_text for key in ["代练中", "服务中", "已拒绝", "拒绝"]), (
        f"S-FLOW-029 丸友集拒绝撤销后未看到代练中/服务中或拒绝结果；source_order_no={source_order_no}; "
        f"text={reject_text[:1500]}"
    )

    player_browser_page = login_player_sub_account_by_sms(page, settings, bug_reporter, player_account)
    player_page = WanyoujiFulfillmentPage(player_browser_page, settings.tenant_home_url, bug_reporter)
    player_frame = player_page.open_player_my_orders(source_order_no)
    player_text = player_page.search_player_my_orders(
        player_frame,
        source_order_no,
        order_title,
    )
    player_row_text = player_page.extract_frame_order_row_text(player_frame, source_order_no, order_title)
    assert any(key in player_row_text for key in ["代练中", "服务中", "已接单"]) and "撤销中" not in player_row_text, (
        f"S-FLOW-029 丸友集拒绝撤销后打手侧未恢复代练中/服务中；source_order_no={source_order_no}; "
        f"text={player_text[:1500]}"
    )
    bug_reporter.record_if_errors(
        account=player_account.phone,
        location=f"S-FLOW-029 打手侧拒绝撤销后状态校验：{main_order_no or source_order_no}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-029",
        status="task_player_accepted",
        current_flow_state="player_sub_order_in_progress",
        task_player_phone=player_account.phone,
        task_wanyouji_phone=wanyouji_account.phone,
        player_revoke_rejected_by=wanyouji_account.phone,
        player_revoke_reject_remark=remark,
        wanyouji_player_revoke_reject_text=reject_text[:1500],
        player_after_revoke_rejected_text=player_text[:1500],
        wanyouji_sub_order_status="代练中",
        available_for_cases=["S-FLOW-027", "S-FLOW-033", "S-FLOW-040"],
    )
