from rest_framework import serializers
from .models import Member, BankDetail, CardTemplate, OTPLog, MemberAuditLog
from django.contrib.auth.models import User

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

class MemberAuditLogSerializer(serializers.ModelSerializer):
    admin_user = UserSerializer(read_only=True)
    
    class Meta:
        model = MemberAuditLog
        fields = '__all__'

class MemberSerializer(serializers.ModelSerializer):
    has_pin = serializers.SerializerMethodField()
    audit_logs = MemberAuditLogSerializer(many=True, read_only=True)

    class Meta:
        model = Member
        exclude = ['pin']  # Never expose the PIN hash

    def get_has_pin(self, obj):
        return bool(obj.pin)

class BankDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = BankDetail
        fields = '__all__'

class CardTemplateSerializer(serializers.ModelSerializer):
    class Meta:
        model = CardTemplate
        fields = '__all__'

class OTPLogSerializer(serializers.ModelSerializer):
    class Meta:
        model = OTPLog
        fields = '__all__'
