package com.lesingle.modules.device.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
 * 终端设备登录响应
 */
@Data
@Schema(description = "终端设备登录响应")
public class DeviceLoginVo implements Serializable {

    @Schema(description = "JWT令牌")
    private String token;

    @Schema(description = "用户ID")
    private Long userId;

    @Schema(description = "用户名")
    private String username;

    @Schema(description = "昵称")
    private String nickname;

    @Schema(description = "头像URL")
    private String avatar;

    @Schema(description = "手机号（脱敏）")
    private String phone;

    /**
     * 从 PublicAuthService.loginBySms() 返回的 Map 转换
     */
    @SuppressWarnings("unchecked")
    public static DeviceLoginVo fromAuthResult(Map<String, Object> authResult) {
        DeviceLoginVo vo = new DeviceLoginVo();
        vo.setToken((String) authResult.get("token"));

        Map<String, Object> user = (Map<String, Object>) authResult.get("user");
        if (user != null) {
            vo.setUserId(user.get("id") != null ? ((Number) user.get("id")).longValue() : null);
            vo.setUsername((String) user.get("username"));
            vo.setNickname((String) user.get("nickname"));
            vo.setAvatar((String) user.get("avatar"));
            vo.setPhone((String) user.get("phone"));
        }
        return vo;
    }
}
