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_076
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_076_approve_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-076",
        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-076 使用的发单后撤销中订单；请先执行 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)
    approve_remark = os.getenv(
        "E2E_S_FLOW_REVOKE_APPROVE_REMARK",
        f"S-FLOW-076 丸掌柜同意发单后撤销 {main_order_no or source_order_no}",
    )
    approve_amount = os.getenv("E2E_S_FLOW_REVOKE_APPROVE_AMOUNT", "1")

    order_page = WanzhangguiOrderPage(page, settings.tenant_home_url, bug_reporter)
    order_page.login_main_account(wanzhanggui_phone, wanzhanggui_password)
    approve_text = order_page.approve_workshop_revoke_request(
        main_order_no=main_order_no,
        source_order_no=source_order_no,
        order_title=order_title,
        remark=approve_remark,
        approve_amount=approve_amount,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-076 丸掌柜同意发单后撤销：{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), (
        f"S-FLOW-076 同意撤销后丸友集侧未看到已取消/撤销终态；source_order_no={source_order_no}; "
        f"text={wanyouji_text[:1500]}"
    )

    next_state = "wanyouji_upstream_revoke_approved_terminal" if from_in_progress else "wanyouji_player_dispatch_revoke_approved_cancelled"
    next_status = "upstream_revoke_approved_terminal" if from_in_progress else "player_dispatch_revoke_approved_cancelled"
    next_sub_status = "撤销完成" if from_in_progress else "已取消"
    next_available = ["S-FLOW-072"] if from_in_progress else ["S-FLOW-012"]

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-076",
        status=next_status,
        current_flow_state=next_state,
        wanzhanggui_revoke_approved_by=wanzhanggui_phone,
        wanzhanggui_revoke_approve_remark=approve_remark,
        wanzhanggui_revoke_approve_amount=approve_amount,
        wanzhanggui_revoke_after_player_dispatch_approve_text=approve_text[:1500],
        wanyouji_after_revoke_approved_text=wanyouji_text[:1500],
        wanyouji_sub_order_status=next_sub_status,
        wanzhanggui_sub_order_status="撤销完成" if from_in_progress else "取消订单",
        available_for_cases=next_available,
    )
