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_075
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_075_apply_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-075",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["wanyouji_dispatched_to_player_hall"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-075 使用的丸友集已发打手未接单订单；请先执行 S-FLOW-021")

    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_REVOKE_REASON",
        f"S-FLOW-075 丸友集发单后申请撤销 {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)
    bug_reporter.record_if_errors(account=wanyouji_account.phone, location="S-FLOW-075 丸友集账号登录")
    revoke_list_text = fulfillment_page.apply_revoke_after_player_dispatch_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-075 丸友集发单后申请撤销：{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_status_text = order_page.assert_order_detail_statuses(
        main_order_no=main_order_no,
        source_order_no=source_order_no,
        order_title=order_title,
        expected_sub_order_list_status=["撤销中", "异常中", "服务中"],
        expected_aftersale_absent=True,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-075 丸掌柜订单详情撤销中状态校验：{main_order_no or source_order_no}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-075",
        status="wanyouji_revoke_pending_after_player_dispatch",
        current_flow_state="wanyouji_revoke_pending_after_player_dispatch",
        wanyouji_revoke_applied_by=wanyouji_account.phone,
        wanyouji_revoke_reason=revoke_reason,
        wanyouji_revoke_amount=revoke_amount,
        wanyouji_revoke_after_player_dispatch_text=revoke_list_text[:1500],
        wanzhanggui_revoke_after_player_dispatch_detail_text=detail_status_text[:1500],
        available_for_cases=["S-FLOW-076", "S-FLOW-077"],
    )
