from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from .models import ImageCollection, Image
from .forms import ImageCollectionForm

class CollectionListView(ListView):
    model = ImageCollection
    template_name = 'links/collection_list.html'
    context_object_name = 'collections'
    paginate_by = 12

    def get_template_names(self):
        if self.request.headers.get('HX-Request'):
            return ['links/includes/collection_list_items.html']
        return [self.template_name]

class CollectionDetailView(DetailView):
    model = ImageCollection
    template_name = 'links/collection_detail.html'
    context_object_name = 'collection'

class CollectionCreateView(CreateView):
    model = ImageCollection
    form_class = ImageCollectionForm
    template_name = 'links/collection_form.html'
    success_url = reverse_lazy('collection-list')

    def form_valid(self, form):
        messages.success(self.request, _('Collection created successfully.'))
        return super().form_valid(form)

class CollectionUpdateView(UpdateView):
    model = ImageCollection
    form_class = ImageCollectionForm
    template_name = 'links/collection_form.html'
    success_url = reverse_lazy('collection-list')

    def form_valid(self, form):
        messages.success(self.request, _('Collection updated successfully.'))
        return super().form_valid(form)

class CollectionDeleteView(DeleteView):
    model = ImageCollection
    template_name = 'links/collection_confirm_delete.html'
    success_url = reverse_lazy('collection-list')

    def delete(self, request, *args, **kwargs):
        messages.success(request, _('Collection deleted successfully.'))
        return super().delete(request, *args, **kwargs)

class CollectionSlideshowView(DetailView):
    model = ImageCollection
    template_name = 'links/collection_slideshow.html'
    context_object_name = 'collection'
