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_046
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_046_start_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-046",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["wanzhanggui_waiting_upstream_acceptance"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-046 使用的待验收订单；请先执行 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)
    amount = os.getenv("E2E_S_FLOW_AFTERSALE_AMOUNT", "1")
    reason = f"S-FLOW-046 待验收阶段主单发起售后 {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-046 丸掌柜账号登录")

    before_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_aftersale_absent=True,
    )
    aftersale_detail_text = order_page.start_pending_acceptance_main_order_aftersale(
        main_order_no=main_order_no,
        source_order_no=source_order_no,
        order_title=order_title,
        reason=reason,
        amount=amount,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-046 丸掌柜待验收阶段主单发起售后：{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,
    )
    assert source_order_no in wanyouji_text or order_title in wanyouji_text, (
        f"S-FLOW-046 发起待验收主单售后后丸友集订单列表未找到订单；source_order_no={source_order_no}; "
        f"text={wanyouji_text[:1500]}"
    )
    assert "售后" in wanyouji_text, (
        f"S-FLOW-046 发起待验收主单售后后丸友集订单未进入售后相关状态；source_order_no={source_order_no}; "
        f"text={wanyouji_text[:1500]}"
    )
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-046 丸友集订单列表售后中状态校验：{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-046",
        status="pending_acceptance_aftersale_pending",
        current_flow_state="wanzhanggui_pending_acceptance_aftersale_pending",
        wanzhanggui_main_status="售后中",
        wanzhanggui_sub_order_status="售后中",
        aftersale_started_by=wanzhanggui_phone,
        aftersale_reason=reason,
        aftersale_amount=amount,
        pending_acceptance_aftersale_before_detail_text=before_detail_text[:1500],
        pending_acceptance_aftersale_started_detail_text=aftersale_detail_text[:1500],
        wanyouji_pending_acceptance_aftersale_text=wanyouji_text[:1500],
        available_for_cases=["S-FLOW-047", "S-FLOW-048"],
    )
