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_033
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_033_wanyouji_apply_upstream_revoke_in_progress(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-033",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["player_sub_order_revoke_approved_terminal"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-033 使用的撤销后异常/终态订单；请先执行 S-FLOW-078。")

    source_order_no = order["source_order_no"]
    main_order_no = order.get("main_order_no", "")
    order_title = order.get("order_title", source_order_no)
    wanyouji_account = resolve_wanyouji_account(order)
    revoke_reason = os.getenv(
        "E2E_S_FLOW_UPSTREAM_REVOKE_REASON",
        f"S-FLOW-033 丸友集子单撤销后申请上游撤销 {main_order_no or source_order_no}",
    )
    revoke_amount = os.getenv("E2E_S_FLOW_REVOKE_AMOUNT", "1")

    fulfillment_page = WanyoujiFulfillmentPage(page, settings.tenant_home_url, bug_reporter)
    fulfillment_page.login_main_account(wanyouji_account.phone, wanyouji_account.password)
    revoke_text = fulfillment_page.apply_revoke_upstream_order_from_workshop_list(
        source_order_no,
        order_title,
        revoke_reason,
        revoke_amount=revoke_amount,
    )
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-033 丸友集代练中申请上游撤销：{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)
    detail_text = order_page.assert_order_detail_statuses(
        main_order_no=main_order_no,
        source_order_no=source_order_no,
        order_title=order_title,
        expected_main_status=["异常中", "代练中", "服务中"],
        expected_sub_status=["代练中异常", "异常中", "代练中", "服务中"],
        expected_sub_order_list_status=["撤销中", "异常中", "代练中", "服务中"],
        expected_aftersale_absent=True,
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-033",
        status="wanyouji_upstream_revoke_pending_in_progress",
        current_flow_state="wanyouji_upstream_revoke_pending_in_progress",
        wanyouji_upstream_revoke_applied_by=wanyouji_account.phone,
        wanyouji_upstream_revoke_reason=revoke_reason,
        wanyouji_upstream_revoke_amount=revoke_amount,
        wanyouji_upstream_revoke_text=revoke_text[:1500],
        wanzhanggui_after_upstream_revoke_detail_text=detail_text[:1500],
        available_for_cases=["S-FLOW-017", "S-FLOW-076", "S-FLOW-077"],
    )
