from __future__ import annotations

import pytest
from playwright.sync_api import Page

from e2e.pages.saas_subscription_admin_page import SaasSubscriptionAdminPage


TARGET_APP_CODE = "APP20260519183410083571"
TARGET_RESOURCE_NAME = "结算中心"
TARGET_VERSION_NAME = "1.1"


@pytest.mark.e2e_standalone
def test_appver_flow_004_select_filtered_resources_and_read_back(page: Page, settings, bug_reporter) -> None:
    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.select_app_version(TARGET_VERSION_NAME)
    admin.search_version_resource(TARGET_RESOURCE_NAME)
    admin.select_all_filtered_version_resources()
    admin.save_current_version_resources()
    admin.assert_version_resource_checked(TARGET_RESOURCE_NAME, checked=True)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"APPVER-FLOW-004 全选当前筛选结果并在弹窗内回读：{TARGET_VERSION_NAME}/{TARGET_RESOURCE_NAME}",
    )

    admin.reopen_app_version_management_and_assert_version(TARGET_APP_CODE, TARGET_VERSION_NAME)
    admin.select_app_version(TARGET_VERSION_NAME)
    admin.assert_version_resource_checked(TARGET_RESOURCE_NAME, checked=True)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"APPVER-FLOW-004 重新进入版本管理回读批量绑定：{TARGET_VERSION_NAME}/{TARGET_RESOURCE_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-004 操作日志校验：保存应用版本菜单",
    )
