from __future__ import annotations

import base64
from datetime import datetime
from pathlib import Path

import pytest
from playwright.sync_api import Page

from e2e.pages.product_solution_admin_page import ProductSolutionAdminPage


LOGO_PATH = Path("runtime/prodsol_flow_001_logo.png")


def ensure_logo_asset() -> Path:
    LOGO_PATH.parent.mkdir(parents=True, exist_ok=True)
    if not LOGO_PATH.exists():
        LOGO_PATH.write_bytes(
            base64.b64decode(
                "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII="
            )
        )
    return LOGO_PATH.resolve()


@pytest.mark.e2e_standalone
def test_prodsol_flow_001_fill_product_basic_info_and_go_next(page: Page, settings, bug_reporter) -> None:
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    product_name = f"自动化产品_{timestamp}"
    product_description = "自动化产品描述"
    version_name = f"自动化订阅版本_{timestamp}"
    version_description = "自动化订阅版本描述"
    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 登录")

    admin.create_product_basic_info_and_go_next(
        product_name=product_name,
        product_description=product_description,
        logo_path=ensure_logo_asset(),
    )
    admin.add_subscription_version(version_name, version_description)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"PRODSOL-FLOW-001 填写产品基础信息并新增订阅版本：{product_name}/{version_name}",
    )
