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


PRODUCT_NAME_KEYWORD = "自动化产品_"


@pytest.mark.e2e_standalone
def test_prodsol_flow_002_edit_product_basic_info_and_read_back(page: Page, settings, bug_reporter) -> None:
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    new_product_name = f"自动化产品编辑_{timestamp}"
    new_description = 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 = admin.edit_latest_product_basic_info_and_finish(
        product_name_keyword=PRODUCT_NAME_KEYWORD,
        new_product_name=new_product_name,
        new_description=new_description,
    )
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"PRODSOL-FLOW-002 编辑产品基础信息并列表回读：{product_code}/{product_name}",
    )

    admin.assert_product_edit_operation_log(product_code=product_code)
    bug_reporter.record_if_errors(
        account=settings.admin_username,
        location=f"PRODSOL-FLOW-002 操作日志校验：{product_code}",
    )
