package com.smyhub.infogenie.infogeniebackendjava.controller;

import com.smyhub.infogenie.infogeniebackendjava.dto.response.AuthCheckResponse;
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/auth/check
 * Optional auth — returns logged_in:true with basic user info if a valid token is present.
 */
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @GetMapping("/check")
    public ResponseEntity<AuthCheckResponse> check(HttpServletRequest request) {
        AuthCheckResponse resp = new AuthCheckResponse();
        AuthCenterUser user =
                (AuthCenterUser) request.getAttribute(AuthCenterUser.AUTH_USER_ATTR);

        if (user != null) {
            resp.setLoggedIn(true);
            AuthCheckResponse.UserInfo info = new AuthCheckResponse.UserInfo();
            info.setAccount(user.getAccount());
            info.setUsername(user.getUsername());
            info.setEmail(user.getEmail());
            resp.setUser(info);
        } else {
            resp.setLoggedIn(false);
        }
        return ResponseEntity.ok(resp);
    }
}
