from rest_framework import generics, status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from .models import (
    AboutBanner, AboutService, AboutProcess, AboutJourney
)
from .serializers import (
    AboutBannerSerializer, AboutServiceSerializer,
    AboutProcessSerializer, AboutJourneySerializer,
    AboutPageSerializer
)


class AboutBannerListAPIView(generics.ListAPIView):
    """API view to get all active about banners"""
    queryset = AboutBanner.objects.filter(is_active=True)
    serializer_class = AboutBannerSerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


class AboutBannerDetailAPIView(generics.RetrieveAPIView):
    """API view to get a specific about banner"""
    queryset = AboutBanner.objects.filter(is_active=True)
    serializer_class = AboutBannerSerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


class AboutServiceListAPIView(generics.ListAPIView):
    """API view to get all active about services"""
    queryset = AboutService.objects.filter(is_active=True)
    serializer_class = AboutServiceSerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


class AboutServiceDetailAPIView(generics.RetrieveAPIView):
    """API view to get a specific about service"""
    queryset = AboutService.objects.filter(is_active=True)
    serializer_class = AboutServiceSerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


class AboutProcessListAPIView(generics.ListAPIView):
    """API view to get all active about processes"""
    queryset = AboutProcess.objects.filter(is_active=True)
    serializer_class = AboutProcessSerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


class AboutProcessDetailAPIView(generics.RetrieveAPIView):
    """API view to get a specific about process"""
    queryset = AboutProcess.objects.filter(is_active=True)
    serializer_class = AboutProcessSerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


class AboutJourneyListAPIView(generics.ListAPIView):
    """API view to get all active about journeys"""
    queryset = AboutJourney.objects.filter(is_active=True)
    serializer_class = AboutJourneySerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


class AboutJourneyDetailAPIView(generics.RetrieveAPIView):
    """API view to get a specific about journey"""
    queryset = AboutJourney.objects.filter(is_active=True)
    serializer_class = AboutJourneySerializer
    permission_classes = [AllowAny]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['request'] = self.request
        return context


@api_view(['GET'])
@permission_classes([AllowAny])
def about_page_data(request):
    """
    API endpoint to get all about page data in one request
    Returns banner, service, process, and journey data
    """
    try:
        # Get the first active instance of each section
        banner = AboutBanner.objects.filter(is_active=True).first()
        service = AboutService.objects.filter(is_active=True).first()
        process = AboutProcess.objects.filter(is_active=True).first()
        journey = AboutJourney.objects.filter(is_active=True).first()

        if not all([banner, service, process, journey]):
            return Response(
                {'error': 'Some about page sections are not configured'},
                status=status.HTTP_404_NOT_FOUND
            )

        # Serialize each section
        banner_serializer = AboutBannerSerializer(banner, context={'request': request})
        service_serializer = AboutServiceSerializer(service, context={'request': request})
        process_serializer = AboutProcessSerializer(process, context={'request': request})
        journey_serializer = AboutJourneySerializer(journey, context={'request': request})

        data = {
            'banner': banner_serializer.data,
            'service': service_serializer.data,
            'process': process_serializer.data,
            'journey': journey_serializer.data
        }

        return Response(data, status=status.HTTP_200_OK)

    except Exception as e:
        return Response(
            {'error': f'An error occurred: {str(e)}'},
            status=status.HTTP_500_INTERNAL_SERVER_ERROR
        )