package com.eactive.eai.manage.session;

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

import org.springframework.beans.factory.annotation.Autowired;
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;

import com.eactive.eai.common.session.CacheInfoVO;

/**
 * SessionManager(SessionManagerForIgnite/SessionManagerForEhcache) 캐시 상태 모니터링 API.
 *
 * GET /manage/session/cache/status          → 전체 캐시 요약 (구현체별 XML 텍스트, Ignite/Ehcache)
 * GET /manage/session/cache/all             → 개별 캐시(login/http-login/evict-master/terminal/convert-user-id/socket) 목록
 * GET /manage/session/cache/login           → 로그인(WebSocket) 캐시
 * GET /manage/session/cache/http-login      → HTTP 로그인 캐시
 * GET /manage/session/cache/evict-master    → 싱글 어댑터 evict master 캐시
 * GET /manage/session/cache/terminal        → 단말(ATM 등) 캐시
 * GET /manage/session/cache/convert-user-id → 단말ID → 사용자ID 변환 캐시
 * GET /manage/session/cache/socket          → 소켓 세션 캐시
 * GET /manage/session/cache/outbound-access-token → 아웃바운드 AccessTokenVO 캐시 (Ignite 백엔드에서만 지원)
 * GET /manage/session/master-inst-id        → 현재 evict master 인스턴스명
 */
@RestController
@RequestMapping("/manage/session")
public class SessionCacheManageController {

    @Autowired
    private SessionCacheManageService sessionCacheManageService;

    @GetMapping("/cache/status")
    public ResponseEntity<?> getCacheStatus() {
        return ok(sessionCacheManageService.getCacheStatus());
    }

    @GetMapping("/master-inst-id")
    public ResponseEntity<?> getMasterInstId() {
        return ok(sessionCacheManageService.getMasterInstId());
    }

    @GetMapping("/cache/all")
    public ResponseEntity<?> getAllCaches() {
        List<CacheInfoVO> list = sessionCacheManageService.getAllCaches();
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", list);
        result.put("count", list.size());
        return ResponseEntity.ok(result);
    }

    @GetMapping("/cache/login")
    public ResponseEntity<?> getLoginCache() {
        return ok(sessionCacheManageService.getLoginCache());
    }

    @GetMapping("/cache/http-login")
    public ResponseEntity<?> getHttpLoginCache() {
        return ok(sessionCacheManageService.getHttpLoginCache());
    }

    @GetMapping("/cache/evict-master")
    public ResponseEntity<?> getEvictMasterCache() {
        return ok(sessionCacheManageService.getEvictMasterCache());
    }

    @GetMapping("/cache/terminal")
    public ResponseEntity<?> getTerminalCache() {
        return ok(sessionCacheManageService.getTerminalCache());
    }

    @GetMapping("/cache/convert-user-id")
    public ResponseEntity<?> getConvertUserIdCache() {
        return ok(sessionCacheManageService.getConvertUserIdCache());
    }

    @GetMapping("/cache/socket")
    public ResponseEntity<?> getSocketCache() {
        return ok(sessionCacheManageService.getSocketCache());
    }

    @GetMapping("/cache/outbound-access-token")
    public ResponseEntity<?> getOutboundAccessTokenCache() {
        try {
            return ok(sessionCacheManageService.getOutboundAccessTokenCache());
        } catch (UnsupportedOperationException e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "현재 SessionManager 백엔드는 OutboundAccessToken 캐시를 지원하지 않습니다.");
            return ResponseEntity.ok(result);
        }
    }

    private ResponseEntity<?> ok(Object data) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", data);
        return ResponseEntity.ok(result);
    }
}
