package com.dbnt.kcscbackend.admin.dashboard;

import com.dbnt.kcscbackend.admin.dashboard.service.AdminDashboardService;
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.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/dashboard")
@Tag(name="AdminDashboardController", description = "사이트관리 대시보드")
public class AdminDashboardController extends BaseController {

    private final AdminDashboardService adminDashboardService;

    @Operation(
            summary = "Dashboard 상단 총접속자수 ~ 민원건수 카운트",
            description = "상단 카운트 조회",
            tags = {"AdminDashboardController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/dash-count", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO getDashCount(@AuthenticationPrincipal LoginVO user)
            throws Exception {

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

        resultMap.put("ConnMonthlyCount", adminDashboardService.selectConnMonthly());
        resultMap.put("ErrorMonthlyCount", adminDashboardService.selectErrorMonthly());
        resultMap.put("DocuMonthlyCount", adminDashboardService.selectDocumentMonthly());
        resultMap.put("CompMonthlyCount", adminDashboardService.selectComplaintMonthly());

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


    @Operation(
            summary = "해당년도 메뉴/방문자수 월/요일별 조회",
            description = "해당년도 메뉴/방문자수 월/요일별 조회",
            tags = {"AdminDashboardController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/menu-login", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO getMenuVisit(@AuthenticationPrincipal LoginVO user)
            throws Exception {

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

        resultMap.put("menuMonthlyList", adminDashboardService.selectMenuMonthly());
        resultMap.put("menuDailyList", adminDashboardService.selectMenuDaily());
        resultMap.put("loginMonthlyList", adminDashboardService.selectLoginMonthly());
        resultMap.put("loginDailyList", adminDashboardService.selectLoginDaily());

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


    @Operation(
            summary = "이번주 다운로드 조회",
            description = "이번주 다운로드 조회",
            tags = {"AdminDashboardController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/file", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO getFile(@AuthenticationPrincipal LoginVO user)
            throws Exception {

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

        resultMap.put("fileDailyList", adminDashboardService.selectFileDaily());

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


    @Operation(
            summary = "미답변 문의사항 조회",
            description = "미답변 문의사항 조회",
            tags = {"AdminDashboardController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/question", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO getBbs(@AuthenticationPrincipal LoginVO user)
            throws Exception {

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

        resultMap.put("QuestionList", adminDashboardService.selectQuestion());

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


    @Operation(
            summary = "해당월 접속방법 조회",
            description = "해당월 PC/MOBILE 조회",
            tags = {"AdminDashboardController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/connect-method", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO getConnectMethod(@AuthenticationPrincipal LoginVO user)
            throws Exception {

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

        resultMap.put("connectMethod", adminDashboardService.selectConnectMonthly());

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




}
