from __future__ import annotations

import os
from collections.abc import Callable

import pytest
from playwright.sync_api import Page

from e2e.tests.应用开通授权.test_e2e_auth_001_authorization_closed_loop import (
    test_丸友集生成密钥并绑定丸掌柜后完成权限配置闭环 as run_auth_001,
)
from e2e.tests.订单流转.test_e2e_order_001_manual_order_dispatch_closed_loop import (
    test_丸掌柜手工录单发单并在丸友集抢单大厅验证闭环 as run_order_001,
)
from e2e.tests.推单配置.test_e2e_push_001_wanzhanggui_workshop_authorization import (
    test_丸掌柜新增丸友集工作室授权 as run_push_001,
)
from e2e.tests.推单配置.test_e2e_push_003_dispatch_strategy import test_丸掌柜新增推单配置策略 as run_push_003
from e2e.tests.注册登录权限.test_e2e_saas_001_enterprise_certification import (
    test_租户注册企业并由主站完成商户认证 as run_saas_001,
)
from e2e.tests.应用开通授权.test_e2e_saas_003_wanyouji_app_opening import (
    test_已认证商户申请开通丸友集并由主站审批 as run_saas_003,
)


固定丸掌柜账号 = "19147509308"
固定丸掌柜密码 = "test123456"


def 执行阶段(
    page: Page,
    阶段编号: str,
    阶段说明: str,
    action: Callable[[], None],
    *,
    reset_page: bool = True,
) -> None:
    print("")
    print(f"[E2E-MAIN-001] 开始 {阶段编号}：{阶段说明}")
    if reset_page:
        隔离当前页面(page)
    action()
    print(f"[E2E-MAIN-001] 完成 {阶段编号}：{阶段说明}")


def 隔离当前页面(page: Page) -> None:
    try:
        page.goto("about:blank", wait_until="domcontentloaded")
        page.wait_for_timeout(500)
    except Exception:
        pass


@pytest.mark.e2e_main_001
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_注册开通授权配置发单接单主流程闭环(page: Page, settings, bug_reporter, monkeypatch) -> None:
    monkeypatch.setenv("E2E_PUSH_WANZHANGGUI_PHONE", 固定丸掌柜账号)
    monkeypatch.setenv("E2E_PUSH_WANZHANGGUI_PASSWORD", 固定丸掌柜密码)
    monkeypatch.setenv("E2E_ORDER_WANZHANGGUI_PHONE", 固定丸掌柜账号)
    monkeypatch.setenv("E2E_ORDER_WANZHANGGUI_PASSWORD", 固定丸掌柜密码)
    monkeypatch.setenv("E2E_ORDER_MODE", "手工录单")
    monkeypatch.setenv("E2E_REUSE_TENANT_LOGIN", "1")

    strategy_type = os.getenv("E2E_MAIN_STRATEGY_TYPE", os.getenv("E2E_PUSH_STRATEGY_TYPE", "单平台指定"))
    assert strategy_type in {"单平台指定", "多工作室同时分发", "多平台分发"}, (
        "E2E_MAIN_STRATEGY_TYPE 仅支持：单平台指定、多工作室同时分发、多平台分发，"
        f"当前值：{strategy_type}"
    )
    monkeypatch.setenv("E2E_PUSH_STRATEGY_TYPE", strategy_type)
    monkeypatch.setenv("E2E_ORDER_STRATEGY_TYPE", strategy_type)

    print("")
    print("[E2E-MAIN-001] 主流程启动")
    print(f"[E2E-MAIN-001] 固定丸掌柜账号：{固定丸掌柜账号}")
    print(f"[E2E-MAIN-001] 推单/发单策略类型：{strategy_type}")

    执行阶段(
        page,
        "SAAS-001",
        "注册新商户、提交认证，并由 SaaS 主站审核通过",
        lambda: run_saas_001(page, settings, bug_reporter),
        reset_page=False,
    )
    执行阶段(
        page,
        "SAAS-003",
        "使用刚认证通过的商户登录租户端，申请开通丸友集并由 SaaS 主站审批",
        lambda: run_saas_003(page, settings, bug_reporter),
    )
    执行阶段(
        page,
        "AUTH-001",
        "丸友集生成授权密钥，固定丸掌柜激活密钥，并完成权限配置与回查",
        lambda: run_auth_001(page, settings, bug_reporter),
    )
    执行阶段(
        page,
        "PUSH-001",
        "固定丸掌柜新增丸友集工作室授权",
        lambda: run_push_001(page, settings, bug_reporter),
    )
    执行阶段(
        page,
        "PUSH-003",
        "固定丸掌柜新增推单策略",
        lambda: run_push_003(page, settings, bug_reporter),
    )
    执行阶段(
        page,
        "ORDER-001",
        "固定丸掌柜手工录单发单，并登录丸友集验证订单到达抢单大厅",
        lambda: run_order_001(page, settings, bug_reporter),
    )

    print("[E2E-MAIN-001] 主流程闭环完成")
