from django.db import models
from grapple.models import GraphQLRichText, GraphQLStreamfield, GraphQLString
from wagtail.admin.panels import FieldPanel
from wagtail.fields import RichTextField, StreamField
from wagtail.models import Page
from wagtail.search import index
from wagtail_headless_preview.models import HeadlessMixin

from dnscms.blocks import PageSectionBlock
from dnscms.fields import BASE_BLOCKS
from dnscms.options import ALL_PIGS


class GenericPage(HeadlessMixin, Page):
    subpage_types = ["generic.GenericPage"]
    show_in_menus = True

    lead = RichTextField(features=["link"])
    body = StreamField(BASE_BLOCKS + [("page_section", PageSectionBlock())])

    PIG_CHOICES = [
        ("", "Ingen"),
    ] + ALL_PIGS

    pig = models.CharField(
        max_length=32,
        choices=PIG_CHOICES,
        default="",
        blank=True,
        help_text="Grisen nedi hjørnet.",
    )

    content_panels = Page.content_panels + [
        FieldPanel("lead", heading="Ingress"),
        FieldPanel("body", heading="Innhold"),
        FieldPanel("pig", heading="Gris"),
    ]

    graphql_fields = [
        GraphQLRichText("lead"),
        GraphQLStreamfield("body"),
        GraphQLString("pig", required=True),
    ]

    search_fields = Page.search_fields + [index.SearchField("lead"), index.SearchField("body")]
