package com.smyhub.infogenie.infogeniebackendjava.controller;

import com.smyhub.infogenie.infogeniebackendjava.dto.response.ApiResponse;
import com.smyhub.infogenie.infogeniebackendjava.dto.response.UserProfileResponse;
import com.smyhub.infogenie.infogeniebackendjava.filter.AuthCenterUser;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * GET /api/user/profile
 * Required auth — returns the full user profile from the auth-center token.
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @GetMapping("/profile")
    public ResponseEntity<?> profile(HttpServletRequest request) {
        AuthCenterUser user =
                (AuthCenterUser) request.getAttribute(AuthCenterUser.AUTH_USER_ATTR);
        if (user == null) {
            return ResponseEntity.status(401)
                    .body(ApiResponse.fail("未授权，请先登录"));
        }

        UserProfileResponse profile = new UserProfileResponse();
        profile.setAccount(user.getAccount());
        profile.setUsername(user.getUsername());
        profile.setEmail(user.getEmail());
        profile.setAvatar(user.getAvatar());
        profile.setLevel(user.getLevel());
        profile.setSproutCoins(user.getSproutCoins());
        profile.setCheckinDays(user.getCheckinDays());
        profile.setCheckinStreak(user.getCheckinStreak());

        return ResponseEntity.ok(ApiResponse.ok(profile));
    }
}
