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.订单流转.test_e2e_task_001_external_order_dispatch_to_player import login_player_sub_account_by_sms
from e2e.tests.订单状态流转.penalty_recovery_flow_helpers import (
    resolve_order_for_case,
    resolve_player_account,
    resolve_wanyouji_account,
)
from e2e.utils.account_store import account_store
from e2e.utils.dual_browser import assert_distinct_account_contexts


@pytest.mark.s_flow_043
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_043_accept_player_sub_order(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)
    expected_amount = os.getenv("E2E_DELIVERY_EXPECTED_PLAYER_AMOUNT", "70")
    store = account_store()
    order = resolve_order_for_case(
        case_id="S-FLOW-043",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=["player_delivery_pending_wanyouji_acceptance"],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-043 使用的打手待验收订单；请先执行 S-FLOW-040。")

    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)
    player_account = resolve_player_account(order, wanyouji_account)

    fulfillment_page = WanyoujiFulfillmentPage(page, settings.tenant_home_url, bug_reporter)
    fulfillment_page.login_main_account(wanyouji_account.phone, wanyouji_account.password)
    wanyouji_frame = fulfillment_page.open_workshop_order_list()
    before_text = fulfillment_page.search_workshop_order_list(
        wanyouji_frame,
        source_order_no,
        order_title,
    )
    assert any(key in before_text for key in ["待验收", "验: 1", "验：1"]), (
        f"S-FLOW-043 前置不满足打手子单待验收；source_order_no={source_order_no}; text={before_text[:1500]}"
    )
    accepted_text = fulfillment_page.accept_sub_order_and_verify_ready_for_upstream(
        source_order_no,
        order_title,
        frame=wanyouji_frame,
        current_text=before_text,
    )
    bug_reporter.record_if_errors(
        account=wanyouji_account.phone,
        location=f"S-FLOW-043 丸友集同意验收打手子单：{main_order_no or source_order_no}",
    )

    player_browser_page = login_player_sub_account_by_sms(page, settings, bug_reporter, player_account)
    assert_distinct_account_contexts(fulfillment_page.page, wanyouji_account.phone, player_browser_page, player_account.phone)
    player_page = WanyoujiFulfillmentPage(player_browser_page, settings.tenant_home_url, bug_reporter)
    income_text = player_page.verify_player_income_for_order(source_order_no, order_title, expected_amount=expected_amount)
    bug_reporter.record_if_errors(
        account=player_account.phone,
        location=f"S-FLOW-043 打手收入校验：{source_order_no}/{order_title}",
    )

    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,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=f"S-FLOW-043 丸掌柜订单详情仍服务中校验：{main_order_no or source_order_no}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-043",
        status="sub_order_income_verified",
        current_flow_state="player_income_ready_for_settlement",
        task_player_phone=player_account.phone,
        task_wanyouji_phone=wanyouji_account.phone,
        sub_order_accepted_by=wanyouji_account.phone,
        settlement_verified_by=player_account.phone,
        expected_player_amount=expected_amount,
        wanyouji_before_sub_order_accept_text=before_text[:1500],
        wanyouji_after_sub_order_accept_text=accepted_text[:1500],
        player_income_after_sub_order_accept_text=income_text[:1500] if isinstance(income_text, str) else "",
        wanzhanggui_after_sub_order_accept_detail_text=detail_text[:1500],
        player_order_status="已完成",
        wanyouji_sub_order_status="已完成",
        wanzhanggui_main_status="服务中",
        wanzhanggui_sub_order_status="代练中",
        available_for_cases=["S-FLOW-080", "S-FLOW-045", "S-FLOW-054"],
    )
