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


@pytest.mark.s_flow_072
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_072_open_penalty_recovery_from_terminal_sub_order(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-072",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=[
            "wanyouji_upstream_revoke_approved_terminal",
            "wanyouji_upstream_revoke_rejected_completed_terminal",
            "player_sub_order_revoke_approved_terminal",
            "wanyouji_sub_order_accepted_ready_for_upstream",
            "wanzhanggui_upstream_acceptance_completed",
        ],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-072 使用的已完成/撤销完成子单；请先执行 S-FLOW-078 或完成验收链路。")

    source_order_no = order["source_order_no"]
    order_title = order.get("order_title", source_order_no)
    main_order_no = order.get("main_order_no", "")
    wanyouji_account = resolve_wanyouji_account(order)
    order_type = os.getenv("E2E_S_FLOW_072_ORDER_TYPE", "追缴单").strip()
    assert order_type in {"追缴单", "处罚单"}, f"E2E_S_FLOW_072_ORDER_TYPE 仅支持追缴单/处罚单；当前值：{order_type}"
    amount = os.getenv("E2E_S_FLOW_RECOVERY_AMOUNT" if order_type == "追缴单" else "E2E_S_FLOW_PENALTY_AMOUNT", "1")
    reason = os.getenv(
        "E2E_S_FLOW_RECOVERY_REASON" if order_type == "追缴单" else "E2E_S_FLOW_PENALTY_REASON",
        f"S-FLOW-072 创建{order_type} {main_order_no or source_order_no}",
    )
    content = os.getenv("E2E_S_FLOW_RECOVERY_CONTENT", f"S-FLOW-072 追缴内容 {main_order_no or source_order_no}")

    fulfillment_page = WanyoujiFulfillmentPage(page, settings.tenant_home_url, bug_reporter)
    fulfillment_page.login_main_account(wanyouji_account.phone, wanyouji_account.password)
    if order_type == "追缴单":
        create_text = fulfillment_page.create_recovery_order_from_terminal_sub_order(
            source_order_no,
            order_title,
            reason=reason,
            content=content,
            amount=amount,
        )
        next_state = "wanyouji_recovery_order_created"
        next_cases = ["S-FLOW-073"]
    else:
        create_text = fulfillment_page.create_direct_penalty_from_terminal_sub_order(
            source_order_no,
            order_title,
            reason=reason,
            amount=amount,
        )
        next_state = "wanyouji_direct_penalty_created"
        next_cases = ["S-FLOW-074"]
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-072 丸友集创建{order_type}：{main_order_no or source_order_no}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-072",
        status=next_state,
        current_flow_state=next_state,
        wanyouji_penalty_recovery_entry_by=wanyouji_account.phone,
        wanyouji_penalty_recovery_type=order_type,
        wanyouji_penalty_recovery_reason=reason,
        wanyouji_penalty_recovery_content=content,
        wanyouji_penalty_recovery_amount=amount,
        wanyouji_penalty_recovery_create_text=create_text[:1500],
        available_for_cases=next_cases,
    )
