from rest_framework import serializers
from django.contrib.auth.models import User
from .models import Profile, Opportunity, IntroductionRequest
from django.contrib.auth.models import User

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

class ProfileSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    tags = serializers.SerializerMethodField()

    class Meta:
        model = Profile
        fields = ['id', 'user', 'bio', 'interests', 'industry', 'is_verified', 'tags']

    def get_tags(self, obj: Profile):
        try:
            return list(obj.tags.names())
        except Exception:
            return []

    

class OpportunitySerializer(serializers.ModelSerializer):
    class Meta:
        model = Opportunity
        fields = ['id', 'title', 'description', 'created_at']

class IntroductionRequestSerializer(serializers.ModelSerializer):
    from_user = UserSerializer(read_only=True)
    to_user = UserSerializer(read_only=True)

    class Meta:
        model = IntroductionRequest
        fields = ['id', 'from_user', 'to_user', 'message', 'is_accepted', 'created_at']


class LeaderRowSerializer(serializers.Serializer):
    rank = serializers.IntegerField()
    user_id = serializers.IntegerField(source='id')
    username = serializers.CharField()
    points = serializers.IntegerField(source='points_given')
    last_help_at = serializers.DateTimeField(allow_null=True)
