package com.dbnt.kcscbackend.admin.dashboard;

import com.dbnt.kcscbackend.admin.dashboard.dto.MonthlyUserLogDTO;
import com.dbnt.kcscbackend.admin.dashboard.service.AdminDashboardService;
import com.dbnt.kcscbackend.config.common.BaseController;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    private final AdminDashboardService adminDashboardService;

    @Operation(
            summary = "일별 사용자 현황 차트 조회",
            description = "일별 사용자 현황 차트 조회",
            tags = {"AdminDashboardController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.GET, value = "/daily-user-log-list", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO getDailyUserLogList() throws Exception {
        ResultVO resultVO = new ResultVO();
        Map<String, Object> resultMap = new HashMap<>();

        // 현재 날짜
        // todo endDate 뒤에 .minus 지워야함
        LocalDate endDate = LocalDate.now().minusMonths(6);
        // 3개월 전 날짜 계산
        LocalDate startDate = endDate.minusMonths(3);

        resultMap.put("dailyUserLogList", adminDashboardService.selectDailyUserLogList(startDate, endDate));
        resultVO.setResult(resultMap);
        return resultVO;
    }

    @Operation(
            summary = "월별 사용자 현황 차트 조회",
            description = "월별 사용자 현황 차트 조회",
            tags = {"AdminDashboardController"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "조회 성공"),
            @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
    })
    @RequestMapping(method = RequestMethod.GET, value = "/monthly-user-log-list", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultVO getMonthlyUserLogList() throws Exception {
        ResultVO resultVO = new ResultVO();
        Map<String, Object> resultMap = new HashMap<>();

        // 현재 날짜
        // todo endDate 뒤에 .minus 지워야함
        LocalDate endDate = LocalDate.now().minusMonths(6);
        // 3개월 전 날짜 계산
        LocalDate startDate = endDate.minusMonths(3);

        List<Object[]> result = adminDashboardService.selectMonthlyUserLogList(startDate, endDate);
        List<MonthlyUserLogDTO> monthlyUserLogDTOList = result.stream()
                .map(row -> new MonthlyUserLogDTO((String) row[0], (BigInteger) row[1]))
                .collect(Collectors.toList());

        resultMap.put("dailyUserLogList", monthlyUserLogDTOList);
        resultVO.setResult(resultMap);
        return resultVO;
    }
}
