package com.dbnt.kcscbackend.admin.logs;

import com.dbnt.kcscbackend.admin.logs.entity.ThPrivacyLog;
import com.dbnt.kcscbackend.admin.logs.service.AdminFileService;
import com.dbnt.kcscbackend.admin.logs.service.AdminMenuService;
import com.dbnt.kcscbackend.admin.logs.service.AdminLogsService;
import com.dbnt.kcscbackend.admin.logs.service.AdminUserService;
import com.dbnt.kcscbackend.auth.entity.LoginVO;
import com.dbnt.kcscbackend.config.common.BaseController;
import com.dbnt.kcscbackend.config.common.ResponseCode;
import com.dbnt.kcscbackend.config.common.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/logs")
@Tag(name="AdminLogsController", description = "사이트관리 로그현황 메뉴 컨트롤러")
public class AdminLogsController extends BaseController {

    private final AdminMenuService adminMenuService;
    private final AdminUserService adminUserService;
    private final AdminLogsService adminLogsService;
    private final AdminFileService adminFileService;

    @Operation(
        summary = "로그현황 - 메뉴별 접속현황",
        description = "메뉴별 접속현황",
        tags = {"AdminLogsController"}
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "조회 성공"),
        @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/menu", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO MenuListCount(@RequestBody Map<String, String> dateRange,
                                  @AuthenticationPrincipal LoginVO user)
            throws Exception {

        ResultVO resultVO = new ResultVO();
        Map<String, Object> resultMap = new HashMap<>();

        String startDate = dateRange.get("start_date");
        String endDate = dateRange.get("end_date");

        resultMap.put("resultCnt", adminMenuService.selectMenuCountCnt(startDate, endDate));
        resultMap.put("resultList", adminMenuService.selectMenuCount(startDate, endDate));
        resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
        resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
        resultVO.setResult(resultMap);
        return resultVO;
    }


    @Operation(
            summary = "로그현황 - 사용자 접속현황",
            description = "사용자 접속현황",
            tags = {"AdminLogsController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/user", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO UserListCount(@RequestBody Map<String, String> dateRange,
                                  @AuthenticationPrincipal LoginVO user)
            throws Exception {

        ResultVO resultVO = new ResultVO();
        Map<String, Object> resultMap = new HashMap<>();

        String startDate = dateRange.get("start_date");
        String endDate = dateRange.get("end_date");

        resultMap.put("resultCnt", adminUserService.selectUserCountCnt(startDate, endDate));
        resultMap.put("resultList", adminUserService.selectUserCount(startDate, endDate));
        resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
        resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
        resultVO.setResult(resultMap);
        return resultVO;
    }


    @Operation(
            summary = "로그현황 - 개인정보 로그",
            description = "개인정보 로그현황",
            tags = {"AdminLogsController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/privacy", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO selectPrivacyLogsList(@RequestBody ThPrivacyLog thPrivacyLog, @AuthenticationPrincipal LoginVO user)
            throws Exception {

        ResultVO resultVO = new ResultVO();
        thPrivacyLog.setQueryInfo();
        Map<String, Object> resultMap = adminLogsService.selectPrivacyList();
        int totCnt = Integer.parseInt((String)resultMap.get("resultCnt"));
        thPrivacyLog.setContentCnt(totCnt);
        thPrivacyLog.setPaginationInfo();
        resultMap.put("paginationInfo", thPrivacyLog);

        resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
        resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
        resultVO.setResult(resultMap);
        return resultVO;
    }


    @Operation(
            summary = "로그현황 - 파일 다운 현황",
            description = "파일 다운 현황",
            tags = {"AdminLogsController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/file", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO FileListCount(@RequestBody Map<String, String> dateRange,
                                  @AuthenticationPrincipal LoginVO user)
            throws Exception {

        ResultVO resultVO = new ResultVO();
        Map<String, Object> resultMap = new HashMap<>();

        String startDate = dateRange.get("start_date");
        String endDate = dateRange.get("end_date");

        resultMap.put("resultCnt", adminFileService.selectFileCountCnt(startDate, endDate));
        resultMap.put("resultList", adminFileService.selectFileCount(startDate, endDate));
        resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
        resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
        resultVO.setResult(resultMap);
        return resultVO;
    }
}
