from django.db import models
from wagtail.contrib.settings.models import BaseSiteSetting, register_setting
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
from wagtail.images import get_image_model


@register_setting
class TenantProfile(BaseSiteSetting):
    """
    Global business configuration per Site (Tenant)
    """

    # =========================
    # Branding
    # =========================
    logo = models.ForeignKey(
        get_image_model(),
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        help_text="Logo utama bisnis (digunakan di FE)"
    )

    # =========================
    # Business Info
    # =========================
    description = models.TextField(
        blank=True,
        help_text="Deskripsi singkat bisnis"
    )

    website = models.URLField(
        blank=True,
        help_text="Website utama (https://...)"
    )

    # =========================
    # Location
    # =========================
    address = models.CharField(
        max_length=255,
        blank=True,
        help_text="Alamat bisnis"
    )

    latitude = models.DecimalField(
        max_digits=9,
        decimal_places=6,
        null=True,
        blank=True
    )

    longitude = models.DecimalField(
        max_digits=9,
        decimal_places=6,
        null=True,
        blank=True
    )

    # =========================
    # Social Media
    # =========================
    socials = models.JSONField(
        default=list,
        blank=True,
        help_text="Daftar URL social media"
    )

    panels = [
        MultiFieldPanel(
            [
                FieldPanel("logo"),
            ],
            heading="Branding"
        ),
        MultiFieldPanel(
            [
                FieldPanel("description"),
                FieldPanel("website"),
            ],
            heading="Business Information"
        ),
        MultiFieldPanel(
            [
                FieldPanel("address"),
                FieldPanel("latitude"),
                FieldPanel("longitude"),
            ],
            heading="Location"
        ),
        MultiFieldPanel(
            [
                FieldPanel("socials"),
            ],
            heading="Social Media"
        ),
    ]

    class Meta:
        verbose_name = "Tenant Profile"

    # tambahkan di TenantProfile

    api_key = models.CharField(
        max_length=64,
        blank=True,
        help_text="API key untuk akses headless (dibaca via header X-API-KEY)"
    )