package com.testapp.controller;

import java.util.Map;
import java.util.HashMap;

import com.testapp.dto.LoginRequest;
import com.testapp.service.GenesysService;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api")
public class TestController {
	
	//React연결 테스트용
	@PostMapping("/ip")
	public ResponseEntity<String> ip (HttpServletRequest request){
		// 요청 보낸 클라이언트 ip 반환
		return ResponseEntity.ok(request.getRemoteAddr());
	}
	
	@PostMapping("/login")
	public ResponseEntity<?> authenticateUser(@RequestBody LoginRequest loginRequest) {
		//서비스 레이어를 통해 아이디/비밀번호 검증 로직
		//성공 시 JWT 토큰이나 세션 정보 생성
		System.out.println("로그인 시도: " +loginRequest.getUsername());
		
		GenesysService gService = new GenesysService();
		String realName = gService.loginToGenesys(loginRequest.getUsername());
		
		Map<String, Object> response = new HashMap<>();
	    response.put("success", true);
	    response.put("name", realName != null ? realName : "사용자"); // 이름이 없으면 기본값
		
		response.put("accessToken",  "generated-jwt-token-here");
		response.put("tokenType", "Bearer");
		response.put("expiresIn", 3600); // 🕒 유효기간 초 단위 전송 (예: 1시간)
		
		return ResponseEntity.ok(response);
	}
}