from django.db import models
from django.utils.translation import gettext_lazy as _
from grapple.helpers import register_singular_query_field
from grapple.models import (
    GraphQLImage,
    GraphQLRichText,
    GraphQLStreamfield,
    GraphQLString,
)
from wagtail.admin.panels import FieldPanel
from wagtail.fields import RichTextField
from wagtail.models import Page, PageManager
from wagtail.search import index
from wagtail_headless_preview.models import HeadlessMixin

from dnscms.fields import CommonStreamField
from dnscms.wordpress.models import DeferWPFieldsManagerMixin, WPImportedPageMixin


class AssociationPageManager(DeferWPFieldsManagerMixin, PageManager):
    pass


@register_singular_query_field("associationIndex")
class AssociationIndex(HeadlessMixin, Page):
    max_count = 1
    subpage_types = ["associations.AssociationPage"]

    lead = RichTextField(features=["italic", "link"])

    body = CommonStreamField

    content_panels = Page.content_panels + [
        FieldPanel("lead", heading=_("Lead")),
        FieldPanel("body", heading=_("Content")),
    ]

    graphql_fields = [
        GraphQLRichText("lead"),
        GraphQLStreamfield("body"),
    ]

    search_fields = Page.search_fields

    class Meta:
        verbose_name = _("association index")
        verbose_name_plural = _("association indexes")


class AssociationPage(HeadlessMixin, WPImportedPageMixin, Page):
    subpage_types = []
    parent_page_types = ["associations.AssociationIndex"]
    show_in_menus = False

    objects = AssociationPageManager()

    class AssociationType(models.TextChoices):
        FORENING = "forening", _("Association")
        UTVALG = "utvalg", _("Committee")

    excerpt = models.TextField(max_length=512, blank=False)
    lead = RichTextField(features=["italic", "link"], blank=True)
    body = CommonStreamField
    association_type = models.CharField(
        max_length=64, choices=AssociationType, default=AssociationType.FORENING
    )
    logo = models.ForeignKey(
        "images.CustomImage",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
    )
    website_url = models.URLField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel(
            "excerpt",
            heading=_("Excerpt"),
            help_text=_("A very short summary of the content below. Used in listing views."),
        ),
        FieldPanel("lead", heading=_("Lead")),
        FieldPanel("body", heading=_("Content")),
        FieldPanel("logo"),
        FieldPanel("association_type", heading=_("Type")),
        FieldPanel("website_url", heading=_("Website")),
    ]

    graphql_fields = [
        GraphQLString("excerpt"),
        GraphQLRichText("lead"),
        GraphQLStreamfield("body"),
        GraphQLImage("logo"),
        GraphQLString("website_url"),
        GraphQLString("association_type"),
    ]

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

    class Meta:
        verbose_name = _("association")
        verbose_name_plural = _("associations")
