"""apps.home 单元测试：HomePage 页面树结构。

test_home_page_subpage_types_allows_all_top_level_sections 是一条回归测试：
此前 HomePage.subpage_types 曾经只包含 blog.BlogIndexPage，导致 products/cases/
solutions 的 IndexPage 无法通过 Wagtail 管理后台"添加子页面"界面创建（subpage_types/
parent_page_types 只影响后台创建校验，不影响 ORM add_child()，问题一度被脚本化测试掩盖）。
"""
import pytest

from apps.blog.models import BlogIndexPage
from apps.cases.models import CaseStudyIndexPage
from apps.core.models import SimpleContentPage
from apps.home.models import HomePage
from apps.products.models import ProductIndexPage
from apps.solutions.models import SolutionIndexPage

pytestmark = pytest.mark.django_db


def test_home_page_subpage_types_allows_all_top_level_sections():
    expected_types = {
        "blog.BlogIndexPage",
        "products.ProductIndexPage",
        "solutions.SolutionIndexPage",
        "cases.CaseStudyIndexPage",
        "core.SimpleContentPage",
    }

    assert expected_types.issubset(set(HomePage.subpage_types))


@pytest.mark.parametrize(
    "index_model,kwargs",
    [
        (BlogIndexPage, {"title": "博客", "slug": "blog"}),
        (ProductIndexPage, {"title": "产品", "slug": "products"}),
        (SolutionIndexPage, {"title": "解决方案", "slug": "solutions"}),
        (CaseStudyIndexPage, {"title": "案例", "slug": "cases"}),
        (SimpleContentPage, {"title": "隐私政策", "slug": "privacy-policy"}),
    ],
)
def test_each_top_level_section_can_be_created_under_home_in_admin(
    home_page, index_model, kwargs
):
    """`can_create_at` 是 Wagtail 管理后台"添加子页面"按钮实际使用的判断（综合
    parent.subpage_types 与 child.parent_page_types 两个方向），此前的 bug 正是
    仅靠 ORM `add_child()` 无法暴露的——它只影响管理后台，不影响直接调用 ORM。"""
    assert index_model.can_create_at(home_page) is True

    child = index_model(**kwargs)
    home_page.add_child(instance=child)

    assert index_model.objects.child_of(home_page).count() == 1
