"""行业解决方案栏目与详情页。详见 documents/设计方案分析与完善版.md §2.5 页面树结构。"""
from django.db import models
from wagtail.admin.panels import FieldPanel
from wagtail.fields import StreamField
from wagtail.search import index

from apps.core.blocks import COMMON_BLOCKS
from apps.core.models import SEOablePage


class SolutionIndexPage(SEOablePage):
    """解决方案栏目列表页，子页面为 SolutionPage。"""

    intro = models.TextField(blank=True, verbose_name="栏目简介")

    content_panels = SEOablePage.content_panels + [
        FieldPanel("intro"),
    ]

    subpage_types = ["solutions.SolutionPage"]
    parent_page_types = ["home.HomePage"]

    class Meta:
        verbose_name = "解决方案栏目页"

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context["solutions"] = (
            SolutionPage.objects.live().descendant_of(self).order_by("path")
        )
        return context


class SolutionPage(SEOablePage):
    """单个行业解决方案详情页。"""

    industry = models.CharField(max_length=100, blank=True, verbose_name="适用行业")
    summary = models.CharField(max_length=250, blank=True, verbose_name="方案简介")
    cover_image = models.ForeignKey(
        "wagtailimages.Image",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        verbose_name="方案配图",
    )
    body = StreamField(COMMON_BLOCKS, use_json_field=True, blank=True)

    search_fields = SEOablePage.search_fields + [
        index.SearchField("industry"),
        index.SearchField("summary"),
        index.SearchField("body"),
    ]

    content_panels = SEOablePage.content_panels + [
        FieldPanel("industry"),
        FieldPanel("summary"),
        FieldPanel("cover_image"),
        FieldPanel("body"),
    ]

    parent_page_types = ["solutions.SolutionIndexPage"]
    subpage_types = []

    class Meta:
        verbose_name = "解决方案页"
