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_048
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_048_cancel_pending_acceptance_aftersale(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)
    order = resolve_order_for_case(
        case_id="S-FLOW-048",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["wanzhanggui_pending_acceptance_aftersale_pending"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-048 使用的待验收阶段售后中订单；请先执行 S-FLOW-046。")

    source_order_no = order["source_order_no"]
    main_order_no = order.get("main_order_no", "")
    order_title = order.get("order_title", source_order_no)
    reason = f"S-FLOW-048 待验收阶段取消主单售后 {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)
    bug_reporter.record_if_errors(account=wanzhanggui_phone, location="S-FLOW-048 丸掌柜账号登录")

    final_text = order_page.cancel_pending_acceptance_main_order_aftersale(
        main_order_no=main_order_no,
        source_order_no=source_order_no,
        order_title=order_title,
        reason=reason,
    )
    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_main_status=["待验收", "服务中", "代练中", "异常中"],
        expected_sub_status=["待验收", "已完成", "代练中", "异常中", "代练中异常"],
        expected_sub_order_list_status=["待验收", "已完成", "代练中", "异常中", "代练中异常"],
        expected_aftersale_absent=True,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-048 丸掌柜待验收阶段取消主单售后：{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)
    wanyouji_text = fulfillment_page.search_workshop_order_list(
        fulfillment_page.open_workshop_order_list(),
        source_order_no,
        order_title,
    )
    wanyouji_row_text = WanyoujiFulfillmentPage.extract_order_row_text(wanyouji_text, source_order_no)
    assert source_order_no in wanyouji_text or order_title in wanyouji_text, (
        f"S-FLOW-048 取消待验收主单售后后丸友集订单列表未找到订单；source_order_no={source_order_no}; "
        f"text={wanyouji_text[:1500]}"
    )
    assert "售后中" not in wanyouji_row_text, (
        f"S-FLOW-048 取消待验收主单售后后丸友集目标订单仍为售后中；source_order_no={source_order_no}; "
        f"row_text={wanyouji_row_text[:1500]}"
    )
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-048 丸友集订单列表取消售后后状态校验：{main_order_no or source_order_no}",
    )

    account_store().mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-048",
        status="pending_acceptance_aftersale_cancelled",
        current_flow_state="wanzhanggui_abnormal_sub_order_after_pending_acceptance_aftersale_cancelled",
        wanzhanggui_main_status="待验收",
        wanzhanggui_sub_order_status="异常中",
        wanzhanggui_sub_order_list_status="异常中",
        aftersale_cancelled_by=wanzhanggui_phone,
        aftersale_cancel_reason=reason,
        pending_acceptance_aftersale_cancelled_text=final_text[:1500],
        pending_acceptance_aftersale_cancelled_detail_text=detail_status_text[:1500],
        wanyouji_pending_acceptance_aftersale_cancelled_text=wanyouji_text[:1500],
        wanyouji_pending_acceptance_aftersale_cancelled_row_text=wanyouji_row_text[:1500],
        available_for_cases=["S-FLOW-034"],
    )
