from rest_framework import serializers
from .models import Publication, PublicationCategory, Author, Submission, ExternalPublication, CSISPublication


class PublicationSerializer(serializers.ModelSerializer):
    """Serializer for Publication model."""
    author_name = serializers.SerializerMethodField()
    category_name = serializers.SerializerMethodField()

    class Meta:
        model = Publication
        fields = [
            "id", "title", "content", "category", "category_name", "author", "author_name",
            "status", "published_at", "tags", "cover_image", "attachments", "views",
            "created_at", "updated_at"
        ]
        read_only_fields = ["id", "created_at", "updated_at"]

    def get_author_name(self, obj):
        return obj.author.get_full_name() or obj.author.username

    def get_category_name(self, obj):
        if obj.category:
            return obj.category.name
        return None


class PublicationCategorySerializer(serializers.ModelSerializer):
    """Serializer for PublicationCategory model."""

    class Meta:
        model = PublicationCategory
        fields = ["id", "slug", "name", "description", "color"]
        read_only_fields = ["id"]


class AuthorSerializer(serializers.ModelSerializer):
    """Serializer for Author model."""
    user_name = serializers.SerializerMethodField()

    class Meta:
        model = Author
        fields = ["id", "user", "user_name", "bio", "expertise", "social_links", "publications"]
        read_only_fields = ["id"]

    def get_user_name(self, obj):
        return obj.user.get_full_name() or obj.user.username


class SubmissionSerializer(serializers.ModelSerializer):
    """Serializer for Submission model."""
    submitted_by_name = serializers.SerializerMethodField()
    reviewed_by_name = serializers.SerializerMethodField()

    class Meta:
        model = Submission
        fields = [
            "id", "publication", "submitted_by", "submitted_by_name", "status",
            "review_notes", "reviewed_by", "reviewed_by_name", "reviewed_at", "submitted_at"
        ]
        read_only_fields = ["id", "submitted_at"]

    def get_submitted_by_name(self, obj):
        return obj.submitted_by.get_full_name() or obj.submitted_by.username

    def get_reviewed_by_name(self, obj):
        if obj.reviewed_by:
            return obj.reviewed_by.get_full_name() or obj.reviewed_by.username
        return None


class CSISPublicationSerializer(serializers.ModelSerializer):
    """Serializer for CSISPublication snapshots."""

    class Meta:
        model = CSISPublication
        fields = [
            "id", "csis_id", "title", "slug", "date_publish",
            "authors", "topics", "category_name", "category_slug",
            "image", "file_url", "description", "viewed", "download_count",
            "synced_at",
        ]
        read_only_fields = fields


class ExternalPublicationSerializer(serializers.ModelSerializer):
    """Serializer for ExternalPublication model."""

    class Meta:
        model = ExternalPublication
        fields = [
            "id", "title", "source", "url", "authors", "published_date",
            "description", "tags", "created_at"
        ]
        read_only_fields = ["id", "created_at"]