package com.kamco.cd.training.log;

import com.kamco.cd.training.config.api.ApiResponseDto;
import com.kamco.cd.training.log.dto.ErrorLogDto;
import com.kamco.cd.training.log.dto.EventType;
import com.kamco.cd.training.log.service.ErrorLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.time.LocalDate;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "에러 로그", description = "에러 로그 관리 API")
@RequiredArgsConstructor
@RestController
@RequestMapping({"/api/logs/system"})
public class ErrorLogApiController {

  private final ErrorLogService errorLogService;

  @Operation(summary = "에러로그 조회")
  @GetMapping("/error")
  public ApiResponseDto<Page<ErrorLogDto.Basic>> getErrorLogs(
      @RequestParam(required = false) ErrorLogDto.LogErrorLevel logErrorLevel,
      @RequestParam(required = false) EventType eventType,
      @RequestParam(required = false) LocalDate startDate,
      @RequestParam(required = false) LocalDate endDate,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size) {
    ErrorLogDto.ErrorSearchReq searchReq =
        new ErrorLogDto.ErrorSearchReq(
            logErrorLevel, eventType, startDate, endDate, page, size, "created_dttm,desc");
    Page<ErrorLogDto.Basic> result = errorLogService.findLogByError(searchReq);
    return ApiResponseDto.ok(result);
  }
}
