from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from .models import Event, CallForProposal, Publication

class StaticViewSitemap(Sitemap):
    priority = 0.5
    changefreq = 'daily'

    def items(self):
        return ['events:index', 'events:event_list', 'events:cfp_list', 'events:gallery', 
                'events:publications', 'events:archive']

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

class EventSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.7

    def items(self):
        return Event.objects.all()

    def lastmod(self, obj):
        return obj.updated_at if hasattr(obj, 'updated_at') else None
    
    def location(self, obj):
        return reverse('events:event_detail', args=[obj.id])

class CFPSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.7

    def items(self):
        return CallForProposal.objects.filter(status='open')

    def lastmod(self, obj):
        return obj.updated_at
    
    def location(self, obj):
        return reverse('events:cfp_detail', args=[obj.id])

class PublicationSitemap(Sitemap):
    changefreq = "monthly"
    priority = 0.6

    def items(self):
        return Publication.objects.filter(is_public=True)

    def lastmod(self, obj):
        return obj.published_at
    
    def location(self, obj):
        # Assuming you have a publication detail page
        # If not, you can remove this sitemap class
        if hasattr(obj, 'get_absolute_url'):
            return obj.get_absolute_url()
        return reverse('events:publications')
