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"
TARGET_VERSION_NAME = "测试结算中心应用版本"


@pytest.mark.e2e_standalone
def test_appver_flow_002_edit_app_version_and_read_back(page: Page, settings, bug_reporter) -> None:
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    edited_description = f"自动化编辑已有应用版本说明_{timestamp}"

    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.assert_version_visible(TARGET_VERSION_NAME)
    admin.edit_app_version(TARGET_VERSION_NAME, TARGET_VERSION_NAME, edited_description)
    admin.assert_version_visible(TARGET_VERSION_NAME)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"APPVER-FLOW-002 编辑已有应用版本并在版本卡片回读：{TARGET_VERSION_NAME}",
    )

    admin.reopen_app_version_management_and_assert_version(TARGET_APP_CODE, TARGET_VERSION_NAME)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"APPVER-FLOW-002 重新进入版本管理回读：{TARGET_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-002 操作日志校验：编辑应用版本",
    )
