from rest_framework import serializers
from .models import *
class AnnouncementSerializer(serializers.ModelSerializer):
 campus_name=serializers.CharField(source='campus.name',read_only=True)
 class Meta:model=Announcement;fields='__all__'
class ReplySerializer(serializers.ModelSerializer):
 author_name=serializers.SerializerMethodField()
 class Meta:model=ForumReply;fields=['id','topic','author','author_name','body','created_at'];read_only_fields=['author']
 def get_author_name(self,o):
  member=getattr(o.author,'member_profile',None)
  return ' '.join(part for part in (member.first_name,member.other_names,member.last_name) if part) if member else o.author.get_full_name() or o.author.phone_number
class TopicSerializer(serializers.ModelSerializer):
 author_name=serializers.SerializerMethodField();reply_count=serializers.IntegerField(source='replies.count',read_only=True);campus_name=serializers.CharField(source='campus.name',read_only=True);can_manage=serializers.SerializerMethodField()
 class Meta:model=ForumTopic;fields='__all__';read_only_fields=['author']
 def get_author_name(self,o):
  member=getattr(o.author,'member_profile',None)
  return ' '.join(part for part in (member.first_name,member.other_names,member.last_name) if part) if member else o.author.get_full_name() or o.author.phone_number
 def get_can_manage(self,o):
  request=self.context.get('request');return bool(request and (request.user.is_staff or request.user==o.author))
 def validate(self,data):
  request=self.context.get('request');visibility=data.get('visibility',getattr(self.instance,'visibility','GENERAL'))
  if visibility=='CAMPUS' and request and not request.user.campus_id:raise serializers.ValidationError({'visibility':'Your account must be assigned to a campus.'})
  return data
class ServiceSerializer(serializers.ModelSerializer):
 campus_name=serializers.CharField(source='campus.name',read_only=True);attendance_count=serializers.IntegerField(source='registrations.count',read_only=True);registered=serializers.SerializerMethodField()
 class Meta:model=ServiceEvent;fields='__all__';read_only_fields=['created_by']
 def get_registered(self,o):return o.registrations.filter(member=self.context['request'].user).exists()
 def validate(self,data):
  if data.get('scope')==ServiceEvent.Scope.CAMPUS and not data.get('campus'):raise serializers.ValidationError({'campus':'Campus is required for a campus event.'})
  if data.get('scope')==ServiceEvent.Scope.NATIONAL:data['campus']=None
  return data
class AttendanceSerializer(serializers.ModelSerializer):
 event_title=serializers.CharField(source='event.title',read_only=True);starts_at=serializers.DateTimeField(source='event.starts_at',read_only=True);campus_name=serializers.CharField(source='event.campus.name',read_only=True)
 class Meta:model=Attendance;fields='__all__';read_only_fields=['member']
class SermonSerializer(serializers.ModelSerializer):
 campus_name=serializers.CharField(source='campus.name',read_only=True)
 class Meta:model=Sermon;fields='__all__'
class GallerySerializer(serializers.ModelSerializer):
 campus_name=serializers.CharField(source='campus.name',read_only=True)
 class Meta:model=GalleryItem;fields='__all__'
