from rest_framework import serializers
from django.contrib.auth.models import User
from core.models import Bookmark, Team, TeamMembership, BookmarkShare, Extraction, Summary
from taggit.serializers import TagListSerializerField, TaggitSerializer

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email']

class TeamSerializer(serializers.ModelSerializer):
    class Meta:
        model = Team
        fields = ['id', 'name', 'description', 'created_at']

class BookmarkSerializer(TaggitSerializer, serializers.ModelSerializer):
    tags = TagListSerializerField(required=False)
    
    class Meta:
        model = Bookmark
        fields = ['id', 'url', 'title', 'notes', 'is_favorite', 'tags', 'created_at', 'updated_at']
        read_only_fields = ['id', 'created_at', 'updated_at']

    def create(self, validated_data):
        user = self.context['request'].user
        tags = validated_data.pop('tags', [])
        bookmark = Bookmark.objects.create(user=user, **validated_data)
        bookmark.tags.set(tags)
        return bookmark

class ExtractionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Extraction
        fields = ['content_text', 'extracted_at']

class BookmarkDetailSerializer(BookmarkSerializer):
    extraction = ExtractionSerializer(read_only=True)
    
    class Meta(BookmarkSerializer.Meta):
        fields = BookmarkSerializer.Meta.fields + ['extraction']
