from django.views.generic import DetailView, ListView
from django.shortcuts import get_object_or_404
from .models import Company, Manufacturer
from rides.models import Ride
from parks.models import Park
from core.views import SlugRedirectMixin

class CompanyDetailView(SlugRedirectMixin, DetailView):
    model = Company
    template_name = 'companies/company_detail.html'
    context_object_name = 'company'

    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        slug = self.kwargs.get(self.slug_url_kwarg)
        # Try to get by current or historical slug
        return self.model.get_by_slug(slug)[0]

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['parks'] = Park.objects.filter(
            owner=self.object
        ).select_related('owner')
        return context

    def get_redirect_url_pattern(self):
        return 'company_detail'

class ManufacturerDetailView(SlugRedirectMixin, DetailView):
    model = Manufacturer
    template_name = 'companies/manufacturer_detail.html'
    context_object_name = 'manufacturer'

    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        slug = self.kwargs.get(self.slug_url_kwarg)
        # Try to get by current or historical slug
        return self.model.get_by_slug(slug)[0]

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['rides'] = Ride.objects.filter(
            manufacturer=self.object
        ).select_related('park', 'coaster_stats')
        return context

    def get_redirect_url_pattern(self):
        return 'manufacturer_detail'

class CompanyListView(ListView):
    model = Company
    template_name = 'companies/company_list.html'
    context_object_name = 'companies'
    paginate_by = 12

    def get_queryset(self):
        queryset = Company.objects.all()
        
        # Filter by country if specified
        country = self.request.GET.get('country')
        if country:
            queryset = queryset.filter(headquarters__icontains=country)
            
        # Search by name if specified
        search = self.request.GET.get('search')
        if search:
            queryset = queryset.filter(name__icontains=search)
            
        return queryset.order_by('name')

class ManufacturerListView(ListView):
    model = Manufacturer
    template_name = 'companies/manufacturer_list.html'
    context_object_name = 'manufacturers'
    paginate_by = 12

    def get_queryset(self):
        queryset = Manufacturer.objects.all()
        
        # Filter by country if specified
        country = self.request.GET.get('country')
        if country:
            queryset = queryset.filter(headquarters__icontains=country)
            
        # Search by name if specified
        search = self.request.GET.get('search')
        if search:
            queryset = queryset.filter(name__icontains=search)
            
        return queryset.order_by('name')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Add stats for filtering
        context['total_manufacturers'] = self.model.objects.count()
        context['total_rides'] = Ride.objects.filter(
            manufacturer__isnull=False
        ).count()
        context['total_roller_coasters'] = Ride.objects.filter(
            manufacturer__isnull=False,
            category='RC'
        ).count()
        return context
