from __future__ import annotations

from datetime import datetime

import pytest
from playwright.sync_api import Page

from e2e.pages.saas_subscription_admin_page import SaasSubscriptionAdminPage


TARGET_APP_CODE = "APP20260519183410083571"


@pytest.mark.e2e_standalone
def test_appver_flow_001_create_app_version_and_read_back(page: Page, settings, bug_reporter) -> None:
    version_name = f"自动化版本_{datetime.now().strftime('%Y%m%d%H%M%S')}"
    version_description = "自动化新增应用版本"

    admin = SaasSubscriptionAdminPage(page, settings.admin_url)
    admin.login(settings.admin_username, settings.admin_password)
    bug_reporter.record_if_errors(account=settings.admin_username, location="SaaS Admin 登录")

    admin.open_app_version_management(TARGET_APP_CODE)
    admin.create_app_version(version_name, version_description)
    admin.assert_version_visible(version_name)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"APPVER-FLOW-001 新增应用版本并在列表回读：{version_name}",
    )

    admin.reopen_app_version_management_and_assert_version(TARGET_APP_CODE, version_name)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"APPVER-FLOW-001 重新进入版本管理回读：{version_name}",
    )

    admin.assert_operation_log(
        target_type="应用",
        target_code=TARGET_APP_CODE,
        operation_info="新建应用版本",
    )
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location="APPVER-FLOW-001 操作日志校验：新建应用版本",
    )
