from django.conf import settings
from django.contrib.sitemaps import Sitemap
from django.urls import reverse

from blog.models import Post
from careers.models import Project

# Saat situs sudah memakai HTTPS, sitemap harus menuliskan https:// —
# kalau tidak, Google menganggapnya alamat lain dan mengabaikan sebagian URL.
SITEMAP_PROTOCOL = "https" if getattr(settings, "USE_HTTPS", False) else None


class StaticSitemap(Sitemap):
    priority = 0.8
    changefreq = "monthly"
    protocol = SITEMAP_PROTOCOL

    def items(self):
        return [
            "core:home",
            "core:about",
            "core:contact",
            "careers:experience",
            "careers:project_list",
            "blog:list",
        ]

    def location(self, item):
        return reverse(item)


class PostSitemap(Sitemap):
    priority = 0.7
    changefreq = "weekly"
    protocol = SITEMAP_PROTOCOL

    def items(self):
        return Post.published.all()

    def lastmod(self, obj):
        return obj.updated_at


class ProjectSitemap(Sitemap):
    priority = 0.6
    changefreq = "monthly"
    protocol = SITEMAP_PROTOCOL

    def items(self):
        return Project.objects.filter(is_published=True)
