from django.db import models
from grapple.models import (
    GraphQLCollection,
    GraphQLForeignKey,
)
from modelcluster.fields import ParentalKey
from modelcluster.models import ClusterableModel
from wagtail.admin.panels import (
    InlinePanel,
    PageChooserPanel,
)
from wagtail.models import Orderable, Page
from wagtail_headless_preview.models import HeadlessMixin


class HomePage(HeadlessMixin, Page):
    max_count = 1

    content_panels = Page.content_panels + [
        InlinePanel("featured_events", heading="Fremhevede arrangementer")
    ]

    graphql_fields = [
        GraphQLCollection(
            GraphQLForeignKey,
            "featured_events",
            "events.EventPage",
            source="featured_events.event",
            required=True,
            item_required=True,
        ),
    ]

    search_fields = []


class HomePageFeaturedEvents(ClusterableModel, Orderable):
    parent = ParentalKey("home.HomePage", related_name="featured_events")
    event = models.ForeignKey(
        "events.EventPage",
        on_delete=models.CASCADE,
    )

    panels = [
        PageChooserPanel("event", "events.EventPage"),
    ]
