from django.db import models
from django.conf import settings

class SiteLanguageQueryset(models.QuerySet):
    def domain_for_page(self, page):
        site_language = self.filter(locale=page.locale).first()
        if site_language is not None:
            return site_language.site.domain

        return self.get(locale__language_code=settings.LANGUAGE_CODE).site


class SiteLanguageConnection(models.Model):
    objects = SiteLanguageQueryset.as_manager()
    locale = models.ForeignKey(
        "wagtailcore.Locale", on_delete=models.SET_NULL, null=True
    )
    site = models.ForeignKey("sites.Site", on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return "%s %s" % (self.locale, self.site)