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_082
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_082_cancel_upstream_acceptance(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)
    cancel_reason = os.getenv("E2E_S_FLOW_CANCEL_UPSTREAM_ACCEPTANCE_REASON", "E2E取消上游验收")

    store = account_store()
    order = resolve_order_for_case(
        case_id="S-FLOW-082",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["wanzhanggui_waiting_upstream_acceptance"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-082 使用的待上游验收订单；请先执行 S-FLOW-045。")

    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)

    fulfillment_page = WanyoujiFulfillmentPage(page, settings.tenant_home_url, bug_reporter)
    fulfillment_page.login_main_account(wanyouji_account.phone, wanyouji_account.password)
    before_text = fulfillment_page.search_workshop_order_list(
        fulfillment_page.open_workshop_order_list(),
        source_order_no,
        order_title,
    )
    assert fulfillment_page.order_waiting_upstream_acceptance(before_text, source_order_no) or "取消验收" in before_text, (
        f"S-FLOW-082 前置不满足待上游验收/可取消验收；source_order_no={source_order_no}; text={before_text[:1500]}"
    )

    cancelled_text = fulfillment_page.cancel_upstream_acceptance(source_order_no, order_title, cancel_reason)
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-082 丸友集取消上游验收：{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,
    )
    assert "待验收" not in detail_text or any(key in detail_text for key in ["服务中", "代练中", "已完成"]), (
        f"S-FLOW-082 丸掌柜侧取消验收后状态未回退到履约/完成子单可继续状态；"
        f"source_order_no={source_order_no}; detail={detail_text[:1500]}"
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-082 丸掌柜取消验收后状态回查：{main_order_no or source_order_no}",
    )

    fulfillment_page.login_main_account(wanyouji_account.phone, wanyouji_account.password)
    recheck_text = fulfillment_page.search_workshop_order_list(
        fulfillment_page.open_workshop_order_list(),
        source_order_no,
        order_title,
    )
    assert source_order_no in recheck_text or order_title in recheck_text, (
        f"S-FLOW-082 丸友集取消验收后回查未找到订单；source_order_no={source_order_no}; text={recheck_text[:1500]}"
    )
    row_text = fulfillment_page.extract_order_row_text(recheck_text, source_order_no)
    assert "取消验收" not in row_text or "申请上游验收" in row_text, (
        f"S-FLOW-082 丸友集取消验收后仍停留待取消验收状态；source_order_no={source_order_no}; row_text={row_text[:1500]}"
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-082",
        status="upstream_acceptance_cancelled",
        current_flow_state="wanyouji_upstream_acceptance_cancelled_ready_for_reapply",
        upstream_acceptance_cancelled_by=wanyouji_account.phone,
        upstream_acceptance_cancel_reason=cancel_reason,
        wanyouji_before_cancel_upstream_acceptance_text=before_text[:1500],
        wanyouji_after_cancel_upstream_acceptance_text=cancelled_text[:1500],
        wanzhanggui_after_cancel_upstream_acceptance_detail_text=detail_text[:1500],
        wanyouji_after_cancel_upstream_acceptance_recheck_text=recheck_text[:1500],
        wanyouji_parent_status="已完成/可申请上游验收",
        wanzhanggui_main_status="服务中/代练中",
        wanzhanggui_sub_order_status="代练中/服务中/已完成",
        available_for_cases=["S-FLOW-045"],
    )
