from __future__ import annotations

import os

import pytest
from playwright.sync_api import Page

from e2e.pages.wanzhanggui_order_page import WanyoujiFulfillmentPage, WanzhangguiOrderPage
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_077
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_077_reject_revoke_after_player_dispatch(page: Page, settings, bug_reporter) -> None:
    wanzhanggui_phone = os.getenv("E2E_ORDER_WANZHANGGUI_PHONE", settings.wanzhanggui_tenant_phone)
    wanzhanggui_password = os.getenv("E2E_ORDER_WANZHANGGUI_PASSWORD", settings.wanzhanggui_tenant_password)
    store = account_store()
    order = resolve_order_for_case(
        case_id="S-FLOW-077",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=[
            "wanyouji_revoke_pending_after_player_dispatch",
            "wanyouji_upstream_revoke_pending_in_progress",
        ],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-077 使用的发单后撤销中订单；请先执行 S-FLOW-075")

    source_order_no = order["source_order_no"]
    main_order_no = order.get("main_order_no", "")
    order_title = order.get("order_title", source_order_no)
    reject_remark = os.getenv(
        "E2E_S_FLOW_REVOKE_REJECT_REMARK",
        f"S-FLOW-077 丸掌柜拒绝发单后撤销 {main_order_no or source_order_no}",
    )

    order_page = WanzhangguiOrderPage(page, settings.tenant_home_url, bug_reporter)
    order_page.login_main_account(wanzhanggui_phone, wanzhanggui_password)
    reject_text = order_page.reject_workshop_revoke_request(
        main_order_no=main_order_no,
        source_order_no=source_order_no,
        order_title=order_title,
        remark=reject_remark,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-077 丸掌柜拒绝发单后撤销：{main_order_no or source_order_no}",
    )

    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)
    frame = fulfillment_page.open_workshop_order_list()
    wanyouji_text = fulfillment_page.search_workshop_order_list(frame, source_order_no, order_title)
    from_in_progress = order.get("current_flow_state") == "wanyouji_upstream_revoke_pending_in_progress"
    expected_keywords = ["已完成", "完成"] if from_in_progress else ["已取消", "取消订单"]
    assert any(key in wanyouji_text for key in expected_keywords) and "撤销中" not in wanyouji_text, (
        f"S-FLOW-077 拒绝撤销后丸友集侧状态不符合预期；source_order_no={source_order_no}; "
        f"text={wanyouji_text[:1500]}"
    )

    next_state = (
        "wanyouji_upstream_revoke_rejected_completed_terminal"
        if from_in_progress
        else "wanyouji_cancelled_after_revoke_rejected_wait_continue_dispatch"
    )
    next_status = "upstream_revoke_rejected_completed" if from_in_progress else "revoke_rejected_wait_wanyouji_continue_dispatch"
    next_available = ["S-FLOW-072"] if from_in_progress else ["S-FLOW-032"]

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-077",
        status=next_status,
        current_flow_state=next_state,
        wanzhanggui_revoke_rejected_by=wanzhanggui_phone,
        wanzhanggui_revoke_reject_remark=reject_remark,
        wanzhanggui_revoke_after_player_dispatch_reject_text=reject_text[:1500],
        wanyouji_after_revoke_rejected_text=wanyouji_text[:1500],
        wanzhanggui_sub_order_status="已完成" if from_in_progress else "异常中",
        wanyouji_sub_order_status="已完成" if from_in_progress else "已取消",
        available_for_cases=next_available,
    )
