from __future__ import annotations

import os

import pytest
from playwright.sync_api import Page

from e2e.pages.wanzhanggui_order_page import WanzhangguiOrderPage
from e2e.tests.订单状态流转.penalty_recovery_flow_helpers import resolve_order_for_case
from e2e.utils.account_store import account_store


@pytest.mark.s_flow_047
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_047_process_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)

    store = account_store()
    order = resolve_order_for_case(
        case_id="S-FLOW-047",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["wanzhanggui_pending_acceptance_aftersale_pending"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-047 使用的待验收阶段售后中订单；请先执行 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)
    process_amount = os.getenv("E2E_S_FLOW_AFTERSALE_PROCESS_AMOUNT", order.get("aftersale_amount", "1") or "1")
    process_remark = f"S-FLOW-047 待验收阶段主单确认售后 {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-047 丸掌柜账号登录")

    final_text = order_page.process_aftersale_for_order(
        source_order_no,
        order_title,
        main_order_no=main_order_no,
        remark=process_remark,
        amount=process_amount,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-047 丸掌柜待验收阶段确认主单售后：{main_order_no or source_order_no}/{order_title}",
    )

    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_parts=["已售后"],
        expected_aftersale_status="已完成",
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-047 丸掌柜待验收阶段售后完成状态校验：{main_order_no or source_order_no}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-047",
        status="aftersale_completed",
        current_flow_state="wanzhanggui_aftersale_completed",
        wanzhanggui_main_status="已售后",
        wanzhanggui_sub_order_status="已售后",
        aftersale_processed_by=wanzhanggui_phone,
        aftersale_process_remark=process_remark,
        aftersale_process_amount=process_amount,
        pending_acceptance_aftersale_processed_text=final_text[:1500],
        pending_acceptance_aftersale_processed_detail_status_text=detail_status_text[:1500],
        available_for_cases=["S-FLOW-051"],
    )
