from django.contrib.auth import get_user_model
from rest_framework import serializers

User = get_user_model()


class UserProfileSerializer(serializers.ModelSerializer):
    permissions = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = [
            "id", "email", "username", "role", "full_name", "phone",
            "location", "skills", "experience_years", "bio",
            "mfa_enabled", "mfa_method", "resume", "permissions",
            "password_last_changed_at", "password_expiry_date", "password_expired",
        ]
        read_only_fields = [
            "id", "email", "username", "role", "mfa_enabled", "mfa_method", "resume", "permissions",
            "password_last_changed_at", "password_expiry_date", "password_expired",
        ]

    def get_permissions(self, obj):
        return list(obj.get_all_permissions())

    def to_representation(self, instance):
        instance.check_password_expiry()
        return super().to_representation(instance)

