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_049_complete_upstream_acceptance = _case(
    "e2e.tests.订单状态流转.订单验收.test_s_flow_049_complete_upstream_acceptance",
    "test_s_flow_049_complete_upstream_acceptance",
)
_test_s_flow_054_start_penalty_from_accepted_sub_order = _case(
    "e2e.tests.订单状态流转.处罚追缴.test_s_flow_054_start_penalty_from_accepted_sub_order",
    "test_s_flow_054_start_penalty_from_accepted_sub_order",
)
_test_s_flow_055_approve_penalty_from_accepted_sub_order = _case(
    "e2e.tests.订单状态流转.处罚追缴.test_s_flow_055_approve_penalty_from_accepted_sub_order",
    "test_s_flow_055_approve_penalty_from_accepted_sub_order",
)


def _configure_new_percent_dispatch(monkeypatch: pytest.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", "抢单大厅")


@pytest.mark.flow_045
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_flow_045_accepted_sub_order_penalty_approve_chain(page: Page, settings, bug_reporter, monkeypatch) -> None:
    _configure_new_percent_dispatch(monkeypatch)

    _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_049_complete_upstream_acceptance(page, settings, bug_reporter)
    _test_s_flow_054_start_penalty_from_accepted_sub_order(page, settings, bug_reporter)
    _test_s_flow_055_approve_penalty_from_accepted_sub_order(page, settings, bug_reporter)
