from __future__ import annotations

from datetime import datetime

import pytest
from playwright.sync_api import Page

from e2e.pages.product_solution_admin_page import ProductSolutionAdminPage


@pytest.mark.e2e_standalone
def test_prodsol_flow_012_add_version_and_feature_compare_preview(page: Page, settings, bug_reporter) -> None:
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    version_name = f"自动化对比版本_{timestamp}"
    version_description = "自动化新增带应用版本的订阅版本"
    switch_item_name = f"自动化开关项_{timestamp}"
    text_item_name = f"自动化文本项_{timestamp}"
    text_item_value = f"自动化文本值_{timestamp}"

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

    product_code, product_name, selected_app_version_name = admin.add_version_with_app_and_config_feature_compare(
        product_name_keywords=[],
        version_name=version_name,
        version_description=version_description,
        switch_item_name=switch_item_name,
        text_item_name=text_item_name,
        text_item_value=text_item_value,
    )
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=(
            "PRODSOL-FLOW-012 已有版本产品新增带应用版本的订阅版本，并在功能对比配置官网预览回读："
            f"{product_code}/{product_name}/{version_name}/{selected_app_version_name}"
        ),
    )

    admin.assert_product_version_create_operation_log(product_code=product_code)
    admin.assert_feature_compare_config_operation_log(product_code=product_code)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"PRODSOL-FLOW-012 操作日志校验：新建产品版本/保存功能对比配置，{product_code}",
    )
