from django import forms
from django.core.validators import RegexValidator
from django.db import models
from grapple.helpers import register_query_field, register_singular_query_field
from grapple.models import GraphQLImage, GraphQLRichText, GraphQLStreamfield, GraphQLString
from wagtail.admin.panels import (
    FieldPanel,
    TitleFieldPanel,
)
from wagtail.fields import RichTextField, StreamField
from wagtail.models import Page
from wagtail.search import index
from wagtail.snippets.models import register_snippet
from wagtail_headless_preview.models import HeadlessMixin

from contacts.blocks import ContactSectionBlock
from dnscms.blocks import BASE_BLOCKS

PHONE_REGEX_VALIDATOR = RegexValidator(
    regex=r"^\+?1?\d{9,15}$",
    message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.",
)


@register_singular_query_field("contactIndex")
class ContactIndex(HeadlessMixin, Page):
    max_count = 1
    subpage_types = []

    lead = RichTextField(features=["italic", "link"], blank=True)
    body = StreamField(
        BASE_BLOCKS + [("contact_section", ContactSectionBlock(label="Kontaktseksjon"))]
    )

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

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

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


@register_snippet
@register_query_field("contactEntity", "contactEntities")
class ContactEntity(models.Model):
    CONTACT_TYPE_CHOICES = [
        ("person", "Person"),
    ]

    name = models.CharField(
        max_length=128,
        null=False,
        blank=False,
    )
    contact_type = models.CharField(
        max_length=128, choices=CONTACT_TYPE_CHOICES, default="person", blank=False
    )
    title = models.CharField(
        max_length=128,
        null=False,
        blank=True,
    )
    email = models.EmailField(blank=True)
    phone_number = models.CharField(validators=[PHONE_REGEX_VALIDATOR], max_length=17, blank=True)

    image = models.ForeignKey(
        "images.CustomImage",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
    )

    title_widget = forms.TextInput(attrs={"placeholder": "Navn*"})

    panels = [
        TitleFieldPanel("name", heading="Navn", widget=title_widget),
        FieldPanel("contact_type", heading="Kontakttype"),
        FieldPanel("title", heading="Tittel/stilling"),
        FieldPanel("email", heading="E-post"),
        FieldPanel("phone_number", heading="Telefonnummer"),
        FieldPanel("image", heading="Bilde"),
    ]

    graphql_fields = [
        GraphQLString("name", required=True),
        GraphQLString("contact_type", required=True),
        GraphQLString("title", required=False),
        GraphQLString("email", required=False),
        GraphQLString("phone_number", required=False),
        GraphQLImage("image", required=False),
    ]

    class Meta:
        verbose_name = "Contact point"
        verbose_name_plural = "Contact points"
        ordering = ["name"]

    def __str__(self):
        return self.name
