from __future__ import annotations

import os
from datetime import datetime

import pytest
from playwright.sync_api import Page

from e2e.pages.wanzhanggui_push_page import WanzhangguiPushPage
from e2e.utils.account_store import account_store


@pytest.mark.e2e_push_003
@pytest.mark.e2e_ledger
@pytest.mark.e2e_chain
def test_丸掌柜新增推单配置策略(page: Page, settings, bug_reporter) -> None:
    wanzhanggui_phone = os.getenv("E2E_PUSH_WANZHANGGUI_PHONE", "19147509308")
    wanzhanggui_password = os.getenv("E2E_PUSH_WANZHANGGUI_PASSWORD", "test123456")
    strategy_type = os.getenv("E2E_PUSH_STRATEGY_TYPE", "单平台指定")
    assert strategy_type in {"单平台指定", "多工作室同时分发", "多平台分发"}, (
        "E2E_PUSH_STRATEGY_TYPE 仅支持：单平台指定、多工作室同时分发、多平台分发，"
        f"当前值：{strategy_type}"
    )

    store = account_store()
    wanzhanggui_account = store.find(phone=wanzhanggui_phone)
    assert wanzhanggui_account is not None, f"台账中未找到丸掌柜账号 {wanzhanggui_phone}"
    assert wanzhanggui_account.wanzhanggui_status == "opened", (
        f"账号 {wanzhanggui_phone} 未处于已开通丸掌柜状态，"
        f"当前状态：{wanzhanggui_account.wanzhanggui_status}"
    )

    required_workshop_count = 2 if strategy_type in {"多工作室同时分发", "多平台分发"} else 1
    candidate_accounts = store.find_workshop_authorized_accounts(wanzhanggui_phone, 50)
    if len(candidate_accounts) < required_workshop_count:
        pytest.skip(
            f"{strategy_type} 至少需要 {required_workshop_count} 个已完成工作室授权的丸友集账号，"
            f"当前仅找到 {len(candidate_accounts)} 个"
        )
    wanyouji_accounts = candidate_accounts

    strategy_name = f"E2E推单策略{datetime.now().strftime('%m%d%H%M%S')}"
    workshop_enterprise_names = [account.enterprise_name for account in wanyouji_accounts]
    push_page = WanzhangguiPushPage(page, settings.tenant_home_url, bug_reporter)
    push_page.login_main_account(wanzhanggui_phone, wanzhanggui_password)
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location="丸掌柜账号-主账号登录",
    )

    actual_workshop_names = push_page.create_dispatch_strategy(
        strategy_name=strategy_name,
        strategy_type=strategy_type,
        workshop_enterprise_name=workshop_enterprise_names[0],
        workshop_enterprise_names=workshop_enterprise_names,
    )
    bug_reporter.record_if_errors(
        account=wanzhanggui_phone,
        location=(
            "丸掌柜-履约工作室/平台-工作室管理-推单配置-"
            f"新增{strategy_type}策略，工作室：{'、'.join(actual_workshop_names)}"
        ),
    )

    actual_accounts = [
        account
        for account in candidate_accounts
        if any(name in account.enterprise_name or account.enterprise_name in name for name in actual_workshop_names)
    ]
    assert len(actual_accounts) >= required_workshop_count, (
        f"页面实际选中的工作室未能在台账中匹配到账号，页面选择：{actual_workshop_names}"
    )

    for wanyouji_account in actual_accounts[:required_workshop_count]:
        store.mark_dispatch_strategy_created(
            wanyouji_account,
            wanzhanggui_phone=wanzhanggui_phone,
            strategy_name=strategy_name,
            strategy_type=strategy_type,
        )
