from __future__ import annotations

from importlib import import_module

import pytest
from playwright.sync_api import Page

from e2e.utils.data_factory import configure_random_percent_dispatch


def _case(module_path: str, function_name: str):
    return getattr(import_module(module_path), function_name)


_test_s_flow_007_create_order_with_dispatch = _case(
    "e2e.tests.订单状态流转.上游发单.test_s_flow_007_create_order_with_dispatch",
    "test_s_flow_007_create_order_with_dispatch",
)
_test_s_flow_014_accept_upstream_order_from_hall = _case(
    "e2e.tests.订单状态流转.上游发单.test_s_flow_014_accept_upstream_order_from_hall",
    "test_s_flow_014_accept_upstream_order_from_hall",
)
_test_s_flow_021_dispatch_accepted_order_to_player = _case(
    "e2e.tests.订单状态流转.上游发单.test_s_flow_021_dispatch_accepted_order_to_player",
    "test_s_flow_021_dispatch_accepted_order_to_player",
)
_test_s_flow_026_player_accept_dispatched_order = _case(
    "e2e.tests.订单状态流转.上游发单.test_s_flow_026_player_accept_dispatched_order",
    "test_s_flow_026_player_accept_dispatched_order",
)
_test_s_flow_040_player_submit_acceptance = _case(
    "e2e.tests.订单状态流转.订单验收.test_s_flow_040_player_submit_acceptance",
    "test_s_flow_040_player_submit_acceptance",
)
_test_s_flow_043_accept_player_sub_order = _case(
    "e2e.tests.订单状态流转.订单验收.test_s_flow_043_accept_player_sub_order",
    "test_s_flow_043_accept_player_sub_order",
)
_test_s_flow_045_apply_upstream_acceptance = _case(
    "e2e.tests.订单状态流转.订单验收.test_s_flow_045_apply_upstream_acceptance",
    "test_s_flow_045_apply_upstream_acceptance",
)
_test_s_flow_046_start_pending_acceptance_aftersale = _case(
    "e2e.tests.订单状态流转.丸掌柜发起售后.test_s_flow_046_start_pending_acceptance_aftersale",
    "test_s_flow_046_start_pending_acceptance_aftersale",
)
_test_s_flow_047_process_pending_acceptance_aftersale = _case(
    "e2e.tests.订单状态流转.丸掌柜发起售后.test_s_flow_047_process_pending_acceptance_aftersale",
    "test_s_flow_047_process_pending_acceptance_aftersale",
)
_test_s_flow_051_start_appeal_from_aftersale_completed = _case(
    "e2e.tests.订单状态流转.主单申诉.test_s_flow_051_start_appeal_from_aftersale_completed",
    "test_s_flow_051_start_appeal_from_aftersale_completed",
)
_test_s_flow_052_approve_main_order_appeal = _case(
    "e2e.tests.订单状态流转.主单申诉.test_s_flow_052_approve_main_order_appeal",
    "test_s_flow_052_approve_main_order_appeal",
)


@pytest.mark.flow_028
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_flow_028_pending_acceptance_aftersale_appeal_approve_chain(
    page: Page,
    settings,
    bug_reporter,
    monkeypatch,
) -> None:
    monkeypatch.setenv("E2E_S_FLOW_CREATE_MODE", "create")
    monkeypatch.delenv("E2E_S_FLOW_MAIN_ORDER_NO", raising=False)
    monkeypatch.delenv("E2E_S_FLOW_SOURCE_ORDER_NO", raising=False)
    monkeypatch.delenv("E2E_S_FLOW_ORDER_TITLE", raising=False)
    configure_random_percent_dispatch(monkeypatch)
    monkeypatch.setenv("E2E_TASK_ASSIGNMENT_METHOD", "抢单大厅")

    _test_s_flow_007_create_order_with_dispatch(page, settings, bug_reporter)
    _test_s_flow_014_accept_upstream_order_from_hall(page, settings, bug_reporter)
    _test_s_flow_021_dispatch_accepted_order_to_player(page, settings, bug_reporter)
    _test_s_flow_026_player_accept_dispatched_order(page, settings, bug_reporter)
    _test_s_flow_040_player_submit_acceptance(page, settings, bug_reporter)
    _test_s_flow_043_accept_player_sub_order(page, settings, bug_reporter)
    _test_s_flow_045_apply_upstream_acceptance(page, settings, bug_reporter)
    _test_s_flow_046_start_pending_acceptance_aftersale(page, settings, bug_reporter)
    _test_s_flow_047_process_pending_acceptance_aftersale(page, settings, bug_reporter)
    _test_s_flow_051_start_appeal_from_aftersale_completed(page, settings, bug_reporter)
    _test_s_flow_052_approve_main_order_appeal(page, settings, bug_reporter)
