from django.views.generic import ListView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone
from .models import EditSubmission, PhotoSubmission
from .mixins import ModeratorRequiredMixin

class EditSubmissionListView(ModeratorRequiredMixin, ListView):
    model = EditSubmission
    template_name = 'moderation/admin/edit_submission_list.html'
    context_object_name = 'submissions'
    paginate_by = 20

    def get_queryset(self):
        queryset = super().get_queryset().select_related(
            'user', 'reviewed_by', 'content_type'
        ).order_by('-submitted_at')
        
        # Filter by status
        status = self.request.GET.get('status')
        if status:
            queryset = queryset.filter(status=status)
            
        # Filter by submission type
        submission_type = self.request.GET.get('type')
        if submission_type:
            queryset = queryset.filter(submission_type=submission_type)
            
        return queryset

    def post(self, request, *args, **kwargs):
        submission_id = request.POST.get('submission_id')
        action = request.POST.get('action')
        review_notes = request.POST.get('review_notes', '')
        
        submission = get_object_or_404(EditSubmission, id=submission_id)
        
        if action == 'approve':
            obj = submission.approve(request.user, review_notes)
            message = 'New addition approved successfully.' if submission.submission_type == 'CREATE' else 'Changes approved successfully.'
            return JsonResponse({
                'status': 'success',
                'message': message,
                'redirect_url': obj.get_absolute_url() if hasattr(obj, 'get_absolute_url') else None
            })
        elif action == 'reject':
            submission.reject(request.user, review_notes)
            message = 'New addition rejected.' if submission.submission_type == 'CREATE' else 'Changes rejected.'
            return JsonResponse({
                'status': 'success',
                'message': message
            })
            
        return JsonResponse({
            'status': 'error',
            'message': 'Invalid action.'
        }, status=400)

class PhotoSubmissionListView(ModeratorRequiredMixin, ListView):
    model = PhotoSubmission
    template_name = 'moderation/admin/photo_submission_list.html'
    context_object_name = 'submissions'
    paginate_by = 20

    def get_queryset(self):
        queryset = super().get_queryset().select_related(
            'user', 'reviewed_by', 'content_type'
        ).order_by('-submitted_at')
        
        status = self.request.GET.get('status')
        if status:
            queryset = queryset.filter(status=status)
            
        return queryset

    def post(self, request, *args, **kwargs):
        submission_id = request.POST.get('submission_id')
        action = request.POST.get('action')
        review_notes = request.POST.get('review_notes', '')
        
        submission = get_object_or_404(PhotoSubmission, id=submission_id)
        
        if action == 'approve':
            submission.approve(request.user, review_notes)
            return JsonResponse({
                'status': 'success',
                'message': 'Photo approved successfully.'
            })
        elif action == 'reject':
            submission.reject(request.user, review_notes)
            return JsonResponse({
                'status': 'success',
                'message': 'Photo rejected successfully.'
            })
            
        return JsonResponse({
            'status': 'error',
            'message': 'Invalid action.'
        }, status=400)
