from __future__ import annotations

import os

import pytest
from playwright.sync_api import Page

from e2e.pages.wanzhanggui_order_page import WanzhangguiOrderPage
from e2e.utils.account_store import account_store


@pytest.mark.s_flow_067
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_067_approve_abnormal_main_order_aftersale(page: Page, settings, bug_reporter) -> None:
    pytest.skip("S-FLOW-067 已收束到通用主单确认售后用例 S-FLOW-003，不再单独执行")
    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_s_flow_067_order(wanzhanggui_phone)
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-067 使用的异常中主单售后中订单，请先执行 S-FLOW-065")

    source_order_no = order["source_order_no"]
    main_order_no = order.get("main_order_no", "")
    order_title = order.get("order_title", source_order_no)
    amount = os.getenv("E2E_S_FLOW_AFTERSALE_PROCESS_AMOUNT", order.get("main_aftersale_amount", "1") or "1")
    approve_remark = f"S-FLOW-067 丸掌柜处理异常中主单售后 {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-067 丸掌柜账号登录")

    final_text = order_page.process_aftersale_for_order(
        source_order_no,
        order_title,
        main_order_no=main_order_no,
        remark=approve_remark,
        amount=amount,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-067 丸掌柜处理异常中主单售后：{main_order_no or source_order_no}",
    )

    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_aftersale_status=["已完成", "售后处理完成", "售后结束"],
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-067 丸掌柜回读丸友集确认主单售后后状态：{main_order_no or source_order_no}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-067",
        status="abnormal_main_order_aftersale_approved",
        current_flow_state="wanzhanggui_abnormal_main_order_aftersale_approved",
        main_aftersale_approved_by=wanzhanggui_phone,
        main_aftersale_approve_remark=approve_remark,
        abnormal_main_order_aftersale_approved_text=final_text[:1500],
        abnormal_main_order_aftersale_approved_detail_text=detail_status_text[:1500],
        available_for_cases=[],
    )


def resolve_s_flow_067_order(wanzhanggui_phone: str) -> dict | None:
    explicit_main_order_no = os.getenv("E2E_S_FLOW_MAIN_ORDER_NO", "").strip()
    explicit_source_order_no = os.getenv("E2E_S_FLOW_SOURCE_ORDER_NO", "").strip()
    payload = account_store().load_payload()
    if explicit_main_order_no or explicit_source_order_no:
        for item in payload.get("dispatched_orders", []):
            if wanzhanggui_phone and item.get("wanzhanggui_phone") != wanzhanggui_phone:
                continue
            if explicit_main_order_no and item.get("main_order_no") != explicit_main_order_no:
                continue
            if explicit_source_order_no and item.get("source_order_no") != explicit_source_order_no:
                continue
            return item
        return None

    return account_store().find_order_for_flow_case(
        case_id="S-FLOW-067",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_state="wanzhanggui_abnormal_main_order_aftersale_pending",
    )
