from rest_framework import serializers
from .models import CaseStudy, CaseStudyCategory, Client, CaseStudyImage, CaseStudyProcess


class CaseStudyCategorySerializer(serializers.ModelSerializer):
    """Serializer for case study categories"""
    case_studies_count = serializers.SerializerMethodField()
    
    class Meta:
        model = CaseStudyCategory
        fields = ['id', 'name', 'slug', 'description', 'display_order', 'case_studies_count']
    
    def get_case_studies_count(self, obj):
        return obj.case_studies.filter(published=True).count()


class ClientSerializer(serializers.ModelSerializer):
    """Serializer for clients"""
    logo = serializers.SerializerMethodField()
    
    class Meta:
        model = Client
        fields = ['id', 'name', 'slug', 'logo', 'description', 'website']
    
    def get_logo(self, obj):
        return obj.get_logo_url


class CaseStudyImageSerializer(serializers.ModelSerializer):
    """Serializer for case study gallery images"""
    image = serializers.SerializerMethodField()
    
    class Meta:
        model = CaseStudyImage
        fields = ['id', 'image', 'caption', 'display_order']
    
    def get_image(self, obj):
        return obj.get_image_url


class CaseStudyProcessSerializer(serializers.ModelSerializer):
    """Serializer for case study process steps"""
    class Meta:
        model = CaseStudyProcess
        fields = ['id', 'title', 'description', 'step_number']


class CaseStudyListSerializer(serializers.ModelSerializer):
    """Serializer for case study list view"""
    category_name = serializers.SerializerMethodField()
    category_slug = serializers.SerializerMethodField()
    client_name = serializers.SerializerMethodField()
    thumbnail = serializers.SerializerMethodField()
    excerpt = serializers.SerializerMethodField()
    
    class Meta:
        model = CaseStudy
        fields = [
            'id', 'title', 'slug', 'subtitle', 'excerpt', 'thumbnail',
            'category_name', 'category_slug', 'client_name',
            'published_at', 'created_at', 'updated_at',
            'views_count', 'featured', 'published', 'display_order'
        ]
    
    def get_category_name(self, obj):
        return obj.category.name if obj.category else None
    
    def get_category_slug(self, obj):
        return obj.category.slug if obj.category else None
    
    def get_client_name(self, obj):
        return obj.client.name if obj.client else None
    
    def get_excerpt(self, obj):
        if obj.excerpt:
            return obj.excerpt
        if obj.description:
            # Strip HTML tags and get first 200 characters
            import re
            text = re.sub(r'<[^>]+>', '', obj.description)
            return text[:200] + '...' if len(text) > 200 else text
        return ''
    
    def get_thumbnail(self, obj):
        return obj.get_thumbnail_url


class CaseStudyDetailSerializer(serializers.ModelSerializer):
    """Serializer for case study detail view"""
    category = CaseStudyCategorySerializer(read_only=True)
    client = ClientSerializer(read_only=True)
    thumbnail = serializers.SerializerMethodField()
    featured_image = serializers.SerializerMethodField()
    poster_image = serializers.SerializerMethodField()
    project_image = serializers.SerializerMethodField()
    gallery_images = CaseStudyImageSerializer(many=True, read_only=True)
    process_steps = CaseStudyProcessSerializer(many=True, read_only=True)
    related_case_studies = serializers.SerializerMethodField()
    
    class Meta:
        model = CaseStudy
        fields = [
            'id', 'title', 'slug', 'subtitle', 'description', 'excerpt',
            'thumbnail', 'featured_image', 'poster_image', 'project_image',
            'project_overview', 'site_map_content',
            'category', 'client', 'gallery_images', 'process_steps',
            'meta_description', 'meta_keywords',
            'published', 'featured', 'views_count', 'display_order',
            'published_at', 'created_at', 'updated_at', 'related_case_studies'
        ]
    
    def get_thumbnail(self, obj):
        return obj.get_thumbnail_url
    
    def get_featured_image(self, obj):
        return obj.get_featured_image_url
    
    def get_poster_image(self, obj):
        return obj.get_poster_image_url
    
    def get_project_image(self, obj):
        return obj.get_project_image_url
    
    def get_related_case_studies(self, obj):
        """Get related case studies from the same category"""
        if not obj.category:
            # If no category, get any published case studies
            related = CaseStudy.objects.filter(
                published=True
            ).exclude(id=obj.id)[:3]
        else:
            related = CaseStudy.objects.filter(
                category=obj.category,
                published=True
            ).exclude(id=obj.id)[:3]
        return CaseStudyListSerializer(related, many=True, context=self.context).data

