package incheon.ags.ias.cntn.cntnPrst.web;

import incheon.ags.ias.cntn.cntnPrst.service.CntnPrstService;
import incheon.ags.ias.cntn.cntnPrst.vo.CntnPrstVO;
import incheon.com.security.annotation.RequireRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

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

@Controller
@RequiredArgsConstructor
@Slf4j
@RequireRole(system = "AGS", roles = "ROLE_SUPER_ADMIN", description = "통합관리자 역할 접근 제어")
public class CntnPrstController {

    private final CntnPrstService cntnPrstService;

    @GetMapping("/ags/ias/cntn/cntnPrstList.do")
    public String cntnPrstList(ModelMap model) throws Exception {

        // 1. 오늘의 최근 시간 데이터 조회 (카드용)
        CntnPrstVO latestStats = cntnPrstService.selectCntnPrstLatestHourStats();

        // 2. 시간대별 통계 조회 (차트용)
        List<Map<String, Object>> hourlyStats = cntnPrstService.selectCntnPrstHourlyStats();
        log.info("=== 차트 데이터 조회 결과 ===");
        log.info("hourlyStats size: {}", hourlyStats != null ? hourlyStats.size() : 0);
        if (hourlyStats != null && !hourlyStats.isEmpty()) {
            log.info("첫 번째 데이터: {}", hourlyStats.get(0));
        }

        // 3. 서버별 통계 조회 (테이블용)
        List<Map<String, Object>> serverStats = cntnPrstService.selectCntnPrstServerStats();

        model.addAttribute("latestStats", latestStats);
        model.addAttribute("hourlyStats", hourlyStats);
        model.addAttribute("serverStats", serverStats);

        return "ags/ias/cntn/cntnPrstList";
    }
}