from __future__ import annotations

import os

import pytest
from playwright.sync_api import Page

from e2e.pages.wanzhanggui_order_page import WanyoujiFulfillmentPage
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


@pytest.mark.s_flow_080
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_s_flow_080_player_apply_settlement(page: Page, settings, bug_reporter) -> None:
    wanzhanggui_phone = os.getenv("E2E_ORDER_WANZHANGGUI_PHONE", settings.wanzhanggui_tenant_phone)
    expected_amount = os.getenv("E2E_DELIVERY_EXPECTED_PLAYER_AMOUNT", "70")
    store = account_store()
    order = resolve_order_for_case(
        case_id="S-FLOW-080",
        wanzhanggui_phone=wanzhanggui_phone,
        current_flow_states=[
            "player_income_ready_for_settlement",
            "wanyouji_sub_order_accepted_ready_for_next_dispatch",
        ],
    )
    if not order:
        pytest.skip("台账中未找到可供 S-FLOW-080 使用的已验收打手子单；请先执行 S-FLOW-043。")

    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)

    player_browser_page = login_player_sub_account_by_sms(page, settings, bug_reporter, player_account)
    player_page = WanyoujiFulfillmentPage(player_browser_page, settings.tenant_home_url, bug_reporter)
    bill_text = player_page.apply_player_settlement_for_order(
        source_order_no,
        order_title,
        expected_amount=expected_amount,
        account_name=os.getenv("E2E_PLAYER_PAYMENT_ACCOUNT_NAME", player_account.contact_name or "E2E打手"),
        account_phone=os.getenv("E2E_PLAYER_PAYMENT_ACCOUNT_PHONE", player_account.phone),
        payment_account=os.getenv("E2E_PLAYER_PAYMENT_ACCOUNT_NO", f"e2e{player_account.phone}@example.com"),
        bank_name=os.getenv("E2E_PLAYER_PAYMENT_BANK_NAME", "E2E测试银行"),
        id_card=os.getenv("E2E_PLAYER_PAYMENT_ID_CARD", "110101199001011234"),
    )
    store.upsert(
        player_account,
        player_payment_account_status="ready",
        player_payment_account_verified_by=player_account.phone,
    )
    bug_reporter.record_if_errors(
        account=player_account.phone,
        location=f"S-FLOW-080 打手我的收入发起结算并提交账单：{main_order_no or source_order_no}/{order_title}",
    )

    store.mark_order_consumed_by_flow_case(
        source_order_no=source_order_no,
        case_id="S-FLOW-080",
        status="player_settlement_bill_submitted",
        current_flow_state="player_settlement_bill_submitted",
        task_player_phone=player_account.phone,
        task_wanyouji_phone=wanyouji_account.phone,
        player_settlement_bill_submitted_by=player_account.phone,
        expected_player_amount=expected_amount,
        player_settlement_bill_submit_text=bill_text[:1500],
        available_for_cases=["S-FLOW-081"],
    )
