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

import incheon.ags.ias.cntn.cntnStats.service.CntnStatsService;
import incheon.ags.ias.cntn.cntnStats.vo.CntnStatsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "접속통계", description = "접속 통계 조회 API")
public class CntnStatsApiController {
    private final CntnStatsService cntnStatsService;

    private static final int PAGE_SIZE = 10;

    @GetMapping("/api/v1/statistics/users")
    @Operation(summary = "사용자별 접속 통계", description = "기간, Top 기준 사용자별 접속 통계를 조회합니다")
    public ResponseEntity<Object> users(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Integer top,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo
    ) {
        CntnStatsVO vo = createVO(startDate, endDate, top);

        // 페이징 처리
        int totalCount = cntnStatsService.selectUserWebLogCount(vo);
        PaginationInfo paginationInfo = createPaginationInfo(pageNo, totalCount);
        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setRecordCountPerPage(PAGE_SIZE);

        List<Map<String, Object>> list = cntnStatsService.selectUserWebLog(vo);

        // 차트용 데이터 (상위 10개)
        CntnStatsVO chartVo = createVO(startDate, endDate, 10);
        List<Map<String, Object>> chartData = cntnStatsService.selectUserWebLog(chartVo);

        return ResponseEntity.ok(createPagedResponse(list, chartData, paginationInfo));
    }

    @GetMapping("/api/v1/statistics/systems")
    @Operation(summary = "시스템별 접속 통계", description = "기간, Top 기준 시스템별 접속 통계를 조회합니다")
    public ResponseEntity<Object> systems(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Integer top
    ) {
        return ResponseEntity.ok(cntnStatsService.selectSystemWebLog(createVO(startDate, endDate, top)));
    }

    @GetMapping("/api/v1/statistics/menus")
    @Operation(summary = "메뉴별 접속 통계", description = "기간, Top 기준 메뉴별 접속 통계를 조회합니다")
    public ResponseEntity<Object> menus(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Integer top,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo
    ) {
        CntnStatsVO vo = createVO(startDate, endDate, top);

        // 페이징 처리
        int totalCount = cntnStatsService.selectMenuWebLogCount(vo);
        PaginationInfo paginationInfo = createPaginationInfo(pageNo, totalCount);
        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setRecordCountPerPage(PAGE_SIZE);

        List<Map<String, Object>> list = cntnStatsService.selectMenuWebLog(vo);

        // 차트용 데이터 (상위 10개)
        CntnStatsVO chartVo = createVO(startDate, endDate, 10);
        List<Map<String, Object>> chartData = cntnStatsService.selectMenuWebLog(chartVo);

        return ResponseEntity.ok(createPagedResponse(list, chartData, paginationInfo));
    }

    @GetMapping("/api/v1/statistics/depts")
    @Operation(summary = "부서별 접속 통계", description = "기간, Top 기준 부서별 접속 통계를 조회합니다")
    public ResponseEntity<Object> depts(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Integer top,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo
    ) {
        CntnStatsVO vo = createVO(startDate, endDate, top);

        // 페이징 처리
        int totalCount = cntnStatsService.selectDeptWebLogCount(vo);
        PaginationInfo paginationInfo = createPaginationInfo(pageNo, totalCount);
        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setRecordCountPerPage(PAGE_SIZE);

        List<Map<String, Object>> list = cntnStatsService.selectDeptWebLog(vo);

        // 차트용 데이터 (상위 10개)
        CntnStatsVO chartVo = createVO(startDate, endDate, 10);
        List<Map<String, Object>> chartData = cntnStatsService.selectDeptWebLog(chartVo);

        return ResponseEntity.ok(createPagedResponse(list, chartData, paginationInfo));
    }

    @GetMapping("/api/v1/statistics/dows")
    @Operation(summary = "요일별 접속 통계", description = "기간 기준 요일별 접속 통계를 조회합니다")
    public ResponseEntity<Object> dows(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        return ResponseEntity.ok(cntnStatsService.selectDowWebLog(createVO(startDate, endDate, null)));
    }

    @GetMapping("/api/v1/statistics/hours")
    @Operation(summary = "시간대별 접속 통계", description = "기간 기준 시간대별 접속 통계를 조회합니다")
    public ResponseEntity<Object> hours(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        return ResponseEntity.ok(cntnStatsService.selectHourlyWebLog(createVO(startDate, endDate, null)));
    }

    private CntnStatsVO createVO(String startDate, String endDate, Integer top) {
        CntnStatsVO vo = new CntnStatsVO();
        vo.setStartDate(startDate);
        vo.setEndDate(endDate);
        vo.setTop(top);
        return vo;
    }

    private PaginationInfo createPaginationInfo(int pageNo, int totalCount) {
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(pageNo);
        paginationInfo.setRecordCountPerPage(PAGE_SIZE);
        paginationInfo.setPageSize(10);
        paginationInfo.setTotalRecordCount(totalCount);
        return paginationInfo;
    }

    private Map<String, Object> createPagedResponse(List<Map<String, Object>> list,
                                                     List<Map<String, Object>> chartData,
                                                     PaginationInfo paginationInfo) {
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("chartData", chartData);
        result.put("paginationInfo", paginationInfo);
        return result;
    }
}
