from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from products.models import Product, Category
from django.utils import timezone


class ProductSitemap(Sitemap):
    changefreq = "daily"
    priority = 0.8

    def items(self):
        return Product.objects.filter(is_active=True)

    def lastmod(self, obj):
        return getattr(obj, "updated_at", None)

    def location(self, obj):
        return reverse('frontend:product_detail', args=[obj.slug])


class CategorySitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.6

    def items(self):
        return Category.objects.filter(is_active=True)

    def location(self, obj):
        return reverse('frontend:products_list') + f"?category={obj.id}"


class StaticViewSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.5

    def items(self):
        return ['frontend:index', 'frontend:products_list', 'frontend:categories', 'frontend:about', 'frontend:contact']

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