"""
用户模块序列化器
"""
from rest_framework import serializers
from .models import User, PointsRecord


class UserSerializer(serializers.ModelSerializer):
    """用户序列化器"""

    class Meta:
        model = User
        fields = ['id', 'phone', 'nickname', 'avatar', 'gender', 'birthday', 'points', 'created_at']
        read_only_fields = ['id', 'phone', 'points', 'created_at']


class PointsRecordSerializer(serializers.ModelSerializer):
    """积分流水序列化器"""

    class Meta:
        model = PointsRecord
        fields = ['id', 'amount', 'type', 'description', 'created_at']


class UserDetailSerializer(serializers.ModelSerializer):
    """用户详情序列化器（管理端）"""
    
    class Meta:
        model = User
        fields = ['id', 'phone', 'nickname', 'avatar', 'is_active', 'is_staff', 'created_at', 'updated_at']


class PhoneLoginSerializer(serializers.Serializer):
    """手机号一键登录序列化器（阿里云号码认证）"""

    token = serializers.CharField(help_text='阿里云号码认证 SDK 返回的 accessToken')


class UpdateUserSerializer(serializers.ModelSerializer):
    """更新用户信息序列化器"""

    class Meta:
        model = User
        fields = ['nickname', 'avatar', 'gender', 'birthday']


class SendCodeSerializer(serializers.Serializer):
    """发送验证码序列化器"""
    phone = serializers.RegexField(
        regex=r'^1[3-9]\d{9}$',
        error_messages={'invalid': '手机号格式不正确'}
    )


class CodeLoginSerializer(serializers.Serializer):
    """验证码登录序列化器"""
    phone = serializers.RegexField(
        regex=r'^1[3-9]\d{9}$',
        error_messages={'invalid': '手机号格式不正确'}
    )
    code = serializers.CharField(max_length=6, min_length=6)
