from __future__ import annotations

import os

import pytest
from playwright.sync_api import Page

from e2e.pages.paycenter_config_page import PaycenterConfigPage
from e2e.utils.account_store import E2EAccount, account_store


@pytest.mark.e2e_settle_paycenter_001
@pytest.mark.e2e_chain
def test_B_SETTLE_PAYCENTER_001_丸掌柜支付中心基础配置(page: Page, settings, bug_reporter) -> None:
    paycenter = PaycenterConfigPage(page, settings.tenant_home_url, bug_reporter)
    paycenter.login_main_account(
        os.getenv("E2E_WANZHANGGUI_TENANT_PHONE", settings.wanzhanggui_tenant_phone),
        os.getenv("E2E_WANZHANGGUI_TENANT_PASSWORD", settings.wanzhanggui_tenant_password),
    )
    paycenter.verify_paycenter_config_sections("wanzhanggui")
    bug_reporter.record_if_errors(
        account=os.getenv("E2E_WANZHANGGUI_TENANT_PHONE", settings.wanzhanggui_tenant_phone),
        location="B-SETTLE-PAYCENTER-001 丸掌柜-支付中心-交易应用/交易通道/账号配置",
    )


@pytest.mark.e2e_settle_paycenter_001
@pytest.mark.e2e_chain
def test_B_SETTLE_PAYCENTER_001_wanyouji_payment_center_config(page: Page, settings, bug_reporter, monkeypatch) -> None:
    account = resolve_wanyouji_paycenter_account(settings)
    suffix = os.getenv("E2E_PAYCENTER_SUFFIX", "")
    if not suffix:
        from datetime import datetime

        suffix = datetime.now().strftime("%m%d%H%M%S")

    monkeypatch.setenv("E2E_PAYCENTER_APP_NAME", f"丸友集-结算中心{suffix}")
    monkeypatch.setenv("E2E_PAYCENTER_APP_CODE", f"wyj_dlsettle_test_{suffix}")
    monkeypatch.setenv("E2E_PAYCENTER_CHANNEL_NAME", f"丸友集支付宝提现{suffix}")
    monkeypatch.setenv("E2E_PAYCENTER_CHANNEL_CODE", f"wyj_ali_transfer_{suffix}")
    monkeypatch.setenv("E2E_PAYCENTER_ACCOUNT_CODE", f"alipay_transfer_test_wanyouji_{suffix}")

    paycenter = PaycenterConfigPage(page, settings.tenant_home_url, bug_reporter)
    paycenter.login_main_account(account.phone, account.password)
    paycenter.verify_paycenter_config_sections("wanyouji")
    bug_reporter.record_if_errors(
        account=account.phone,
        location="B-SETTLE-PAYCENTER-001 丸友集-支付中心-交易应用/交易通道/账号配置",
    )


def resolve_wanyouji_paycenter_account(settings) -> E2EAccount:
    explicit_phone = os.getenv("E2E_SETTLE_WANYOUJI_PHONE", "").strip()
    candidates = [
        account
        for account in account_store().load()
        if account.account_type == "main"
        and account.wanyouji_status == "opened"
        and account.certification_status == "approved"
        and (not explicit_phone or account.phone == explicit_phone)
    ]
    candidates = sorted(candidates, key=lambda account: account.updated_at, reverse=True)
    if candidates:
        return candidates[0]
    if explicit_phone:
        raise AssertionError(f"账本中未找到已开通丸友集的指定主账号：{explicit_phone}")
    return E2EAccount(
        phone=settings.certified_tenant_phone,
        password=settings.certified_tenant_password,
        enterprise_name=settings.certified_tenant_enterprise_name,
        contact_name="",
        certification_status="approved",
        wanyouji_status="opened",
        merchant_id=settings.certified_tenant_merchant_id,
    )
