package com.takensoft.cms.cntnStats.web;

import com.takensoft.cms.cntnStats.dto.CntnStatsDTO;
import com.takensoft.cms.cntnStats.dto.CntnStatsExcelDTO;
import com.takensoft.cms.cntnStats.service.CntnStatsService;
import com.takensoft.common.excel.service.ExcelService;
import com.takensoft.common.message.MessageCode;
import com.takensoft.common.util.ResponseData;
import com.takensoft.common.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.*;
/**
 * @author takensoft
 * @since 2024.05.23
 * @modification
 *     since    |    author    | description
 *  2024.05.23  |  takensoft   | 최초 등록
 *
 * 접속 통계 관련 컨트롤러
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping(value = "/sys/cntnStats")
public class CntnStatsController {
    private final CntnStatsService cntnStatsService;
    private final ExcelService excelService;
    private final ResponseUtil resUtil;

    /**
     * @author takensoft
     * @since 2024.05.23
     * @param cntnStatsDTO
     * @return
     * @throws Exception
     *
     * 접속 통게 등록
     */
    @PostMapping("/saveProc.json")
    public ResponseEntity<?> saveProc(@RequestBody CntnStatsDTO cntnStatsDTO) throws Exception {
        int result = cntnStatsService.cntnStatsSave(cntnStatsDTO);

        // 응답 처리
        if(result > 0) {
            return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
        } else {
            return resUtil.errorRes(MessageCode.COMMON_INSERT_FAIL);
        }
    }

    /**
     * @author 박정하
     * @since 2024.05.29
     * @param
     * @return
     * @throws Exception
     *
     * 사용자 접속 통계
     */
    @PostMapping("/selectUserCntnStatsProc.json")
    public ResponseEntity<?> selectUserCntnStatsProc(@RequestBody HashMap<String, Object> params) throws Exception {
        // 사용자 접속 통계 조회
        HashMap<String, Object> result = cntnStatsService.userCntnStatsSelect(params);

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @author 박정하
     * @since 2024.05.30
     * @param
     * @return
     * @throws Exception
     *
     * 메뉴별 접속 통계
     */
    @PostMapping("/selectMenuCntnStatsProc.json")
    public ResponseEntity<?> selectMenuCntnStatsProc(@RequestBody HashMap<String, Object> params) throws Exception {
        // 메뉴별 접속 통계 조회
        HashMap<String, Object> result = cntnStatsService.menuCntnStatsSelect(params);

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @author 박정하
     * @since 2024.05.30
     * @param
     * @return
     * @throws Exception
     *
     * 게시판 접속 통계
     */
    @PostMapping("/selectBbsCntnStatsProc.json")
    public ResponseEntity<?> selectBbsCntnStatsProc(@RequestBody HashMap<String, Object> params) throws Exception {
        // 게시판 접속 통계 조회
        HashMap<String, Object> result = cntnStatsService.bbsCntnStatsSelect(params);

        // 응답 처리
        return resUtil.successRes(result, MessageCode.COMMON_SUCCESS);
    }

    /**
     * @author 박정하
     * @since 2024.05.30
     * @param
     * @return
     * @throws Exception
     *
     * 접속 통계 엑셀 다운로드
     */
    @PostMapping("/excelDownload.file")
    public void excelDownload(HttpServletResponse response, @RequestPart List<CntnStatsExcelDTO> cntnStats, @RequestPart String chart) throws Exception {
        excelService.cntnStatsExcelDownload(response, cntnStats, chart);
    }
}