from django.shortcuts import render
from archive.models import *
from django.views.generic import TemplateView,CreateView
from django.db.models import Prefetch
from meta.views import MetadataMixin
from django.db.models.functions import Length
from django.urls import reverse
from .models import *


class HomeTemplateView(MetadataMixin,TemplateView):
    template_name = 'website/home.html'
    
    def get_context_data(self, **kwargs):
        context =  super().get_context_data(**kwargs)
        context['videos'] = CeremonyFile.objects.filter(file_type='video',is_pin=True).order_by('-uploaded_at')[:10]
        context['audios'] = CeremonyFile.objects.filter(file_type='audio',is_pin=True).order_by('-uploaded_at')[:10]
        context['rozehs'] = CeremonyFile.objects.filter(file_style='rozeh',file_type='video',is_pin=True).order_by('-uploaded_at')[:10]
        
        

        context['sliders'] = (
            CeremonyFile.objects.filter(
                file_type='video',
                is_pin=True,
            )
            .annotate(poster_len=Length('poster_desktop'))
            .filter(poster_len__gt=3)
            .order_by('-updated_at')[:3]
        )
        
        context['occasions'] = Occasion.objects.all().prefetch_related(
            Prefetch(
                'ceremony_file_occasion',
                queryset=CeremonyFile.objects.filter(
                    is_pin=True
                ).order_by('-uploaded_at'),
                to_attr='pinned_files'
            )
        )
        
        
        return context

    def get_meta_title(self, context=None):
        return "سایت رسمی | امین قدیم"

    def get_meta_description(self, context=None):
        return "سایت رسمی کربلایی امین قدیم؛ آرشیو کامل مداحی، روضه‌خوانی، صوت و ویدئوهای مذهبی با امکان دانلود مستقیم و مشاهده آنلاین جدیدترین مراسم‌ها و مناسبت‌های مذهبی."
    
    def get_meta_url(self, context=None):
        return reverse("home")
 

class AboutView(MetadataMixin,TemplateView):
    template_name = "website/about.html"

    def get_meta_title(self, context=None):
        return "درباره ما | امین قدیم"

    def get_meta_description(self, context=None):
        return "آشنایی با کربلایی امین قدیم و فعالیت‌های مذهبی ایشان؛ معرفی اهداف سایت در انتشار و آرشیو مداحی، روضه و مراسم‌های مذهبی برای علاقه‌مندان."

    def get_meta_url(self, context=None):
        return reverse("about")
    
    
from django.urls import reverse, reverse_lazy
from django.contrib import messages

class ContactView(MetadataMixin, CreateView):
    template_name = "website/contact.html"
    model = ContactData
    fields = "__all__"
    success_url = reverse_lazy("contact")
    
    
    def form_valid(self, form):
        messages.success(
            self.request,
            "پیام شما با موفقیت ثبت شد. در اولین فرصت با شما تماس خواهیم گرفت."
        )
        return super().form_valid(form)
    def get_meta_title(self, context=None):
        return "تماس با ما | امین قدیم"

    def get_meta_description(self, context=None):
        return "راه‌های ارتباط با تیم پایگاه امین قدیم برای ارسال پیام، پیشنهاد، انتقاد یا هماهنگی برگزاری مراسم‌های مذهبی."

    def get_meta_url(self, context=None):
        return reverse("contact")

class DesignerView(MetadataMixin,TemplateView):
    template_name = "website/designer.html"

    def get_meta_title(self, context=None):
        return "امیرعلی خلیلی | امین قدیم"

    def get_meta_description(self, context=None):
        return "معرفی امیرعلی خلیلی، طراح و توسعه‌دهنده سایت امین قدیم؛ مسئول طراحی و پیاده‌سازی فنی پایگاه آرشیو مداحی و مراسم‌های مذهبی."

    def get_meta_url(self, context=None):
        return reverse("designer")
    
    
class BioView(MetadataMixin,TemplateView):
    template_name = "website/bio.html"

    def get_meta_title(self, context=None):
        return "صفحات رسمی | امین قدیم"

    def get_meta_description(self, context=None):
        return "دسترسی به صفحات و شبکه‌های اجتماعی رسمی کربلایی امین قدیم؛ پیج اینستاگرام، کانال تلگرام و سایر بسترهای رسمی برای دنبال کردن جدیدترین محتوا."

    def get_meta_url(self, context=None):
        return reverse("bio")
