from rest_framework import serializers
from igny8_core.business.content.models import Content


class LinkContentSerializer(serializers.Serializer):
    """Serializer for linking content"""
    content_id = serializers.IntegerField(required=True)
    
    def validate_content_id(self, value):
        try:
            content = Content.objects.get(id=value)
            account = self.context['request'].user.account if hasattr(self.context['request'].user, 'account') else None
            if account and content.account != account:
                raise serializers.ValidationError("Content not found or access denied")
            return value
        except Content.DoesNotExist:
            raise serializers.ValidationError("Content not found")


class BatchLinkContentSerializer(serializers.Serializer):
    """Serializer for batch linking"""
    content_ids = serializers.ListField(
        child=serializers.IntegerField(),
        min_length=1,
        max_length=50
    )
    
    def validate_content_ids(self, value):
        account = self.context['request'].user.account if hasattr(self.context['request'].user, 'account') else None
        if not account:
            raise serializers.ValidationError("Account not found")
        
        content_ids = Content.objects.filter(
            id__in=value,
            account=account
        ).values_list('id', flat=True)
        
        if len(content_ids) != len(value):
            raise serializers.ValidationError("Some content IDs are invalid or inaccessible")
        
        return list(content_ids)

