from rest_framework import serializers
from .models import BlogPost, BlogCategory, BlogAuthor, BlogTag, BlogComment


class BlogAuthorSerializer(serializers.ModelSerializer):
    """Serializer for blog authors"""
    class Meta:
        model = BlogAuthor
        fields = ['id', 'name', 'email', 'bio', 'avatar']


class BlogCategorySerializer(serializers.ModelSerializer):
    """Serializer for blog categories"""
    posts_count = serializers.SerializerMethodField()
    
    class Meta:
        model = BlogCategory
        fields = ['id', 'title', 'slug', 'description', 'display_order', 'posts_count']
    
    def get_posts_count(self, obj):
        return obj.posts.filter(published=True).count()


class BlogTagSerializer(serializers.ModelSerializer):
    """Serializer for blog tags"""
    class Meta:
        model = BlogTag
        fields = ['id', 'name', 'slug']


class BlogPostListSerializer(serializers.ModelSerializer):
    """Serializer for blog post list view"""
    author_name = serializers.CharField(source='author.name', read_only=True)
    category_title = serializers.CharField(source='category.title', read_only=True)
    category_slug = serializers.CharField(source='category.slug', read_only=True)
    thumbnail = serializers.SerializerMethodField()
    tags = BlogTagSerializer(many=True, read_only=True)
    
    class Meta:
        model = BlogPost
        fields = [
            'id', 'title', 'slug', 'excerpt', 'thumbnail', 
            'author_name', 'category_title', 'category_slug',
            'tags', 'published_at', 'created_at', 'updated_at',
            'views_count', 'reading_time', 'featured', 'published'
        ]
    
    def get_thumbnail(self, obj):
        return obj.get_thumbnail_url


class BlogPostDetailSerializer(serializers.ModelSerializer):
    """Serializer for blog post detail view"""
    author = BlogAuthorSerializer(read_only=True)
    category = BlogCategorySerializer(read_only=True)
    tags = BlogTagSerializer(many=True, read_only=True)
    thumbnail = serializers.SerializerMethodField()
    featured_image = serializers.SerializerMethodField()
    related_posts = serializers.SerializerMethodField()
    
    class Meta:
        model = BlogPost
        fields = [
            'id', 'title', 'slug', 'content', 'excerpt',
            'thumbnail', 'featured_image', 'author', 'category', 'tags',
            'meta_description', 'meta_keywords',
            'published', 'featured', 'views_count', 'reading_time',
            'published_at', 'created_at', 'updated_at', 'related_posts'
        ]
    
    def get_thumbnail(self, obj):
        return obj.get_thumbnail_url
    
    def get_featured_image(self, obj):
        return obj.get_featured_image_url
    
    def get_related_posts(self, obj):
        """Get related posts from the same category"""
        related = BlogPost.objects.filter(
            category=obj.category,
            published=True
        ).exclude(id=obj.id)[:3]
        return BlogPostListSerializer(related, many=True, context=self.context).data


class BlogCommentSerializer(serializers.ModelSerializer):
    """Serializer for blog comments"""
    replies = serializers.SerializerMethodField()
    
    class Meta:
        model = BlogComment
        fields = [
            'id', 'post', 'name', 'email', 'content',
            'parent', 'is_approved', 'created_at', 'updated_at', 'replies'
        ]
        read_only_fields = ['is_approved', 'created_at', 'updated_at']
    
    def get_replies(self, obj):
        """Get nested replies"""
        if obj.replies.exists():
            return BlogCommentSerializer(
                obj.replies.filter(is_approved=True),
                many=True
            ).data
        return []


class BlogCommentCreateSerializer(serializers.ModelSerializer):
    """Serializer for creating blog comments"""
    class Meta:
        model = BlogComment
        fields = ['post', 'name', 'email', 'content', 'parent']

