from rest_framework import serializers
from .models import Activity


class ActivitySerializer(serializers.ModelSerializer):
    """Serializer for Activity model."""
    user_name = serializers.SerializerMethodField()
    contact_name = serializers.SerializerMethodField()
    company_name = serializers.SerializerMethodField()
    project_name = serializers.SerializerMethodField()

    class Meta:
        model = Activity
        fields = [
            "id", "title", "activity_type", "description", "contact", "contact_name",
            "company", "company_name", "project", "project_name", "user", "user_name",
            "due_date", "is_completed", "completed_at", "metadata", "created_at", "updated_at"
        ]
        read_only_fields = ["id", "user", "created_at", "updated_at"]

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

    def get_contact_name(self, obj):
        if obj.contact:
            return obj.contact.name
        return None

    def get_company_name(self, obj):
        if obj.company:
            return obj.company.name
        return None

    def get_project_name(self, obj):
        if obj.project:
            return obj.project.name
        return None