package com.kamco.cd.kamcoback.log;

import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
import com.kamco.cd.kamcoback.log.dto.AuditLogDto;
import com.kamco.cd.kamcoback.log.service.AuditLogService;
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/log/audit")
public class AuditLogApiController {

  private final AuditLogService auditLogService;

  @Operation(summary = "일자별 로그 조회")
  @GetMapping("/daily")
  public ApiResponseDto<Page<AuditLogDto.DailyAuditList>> getDailyLogs(
      @RequestParam(required = false) LocalDate startDate,
      @RequestParam(required = false) LocalDate endDate,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size) {
    AuditLogDto.searchReq searchReq = new AuditLogDto.searchReq(page, size, "created_dttm,desc");

    Page<AuditLogDto.DailyAuditList> result =
        auditLogService.getLogByDaily(searchReq, startDate, endDate);

    return ApiResponseDto.ok(result);
  }

  @Operation(summary = "일자별 로그 상세")
  @GetMapping("/daily/result")
  public ApiResponseDto<Page<AuditLogDto.DailyDetail>> getDailyResultLogs(
      @RequestParam LocalDate logDate,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size) {
    AuditLogDto.searchReq searchReq = new AuditLogDto.searchReq(page, size, "created_dttm,desc");
    Page<AuditLogDto.DailyDetail> result = auditLogService.getLogByDailyResult(searchReq, logDate);

    return ApiResponseDto.ok(result);
  }

  @Operation(summary = "메뉴별 로그 조회")
  @GetMapping("/menu")
  public ApiResponseDto<Page<AuditLogDto.MenuAuditList>> getMenuLogs(
      @RequestParam(required = false) String searchValue,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size) {
    AuditLogDto.searchReq searchReq = new AuditLogDto.searchReq(page, size, "created_dttm,desc");
    Page<AuditLogDto.MenuAuditList> result = auditLogService.getLogByMenu(searchReq, searchValue);

    return ApiResponseDto.ok(result);
  }

  @Operation(summary = "메뉴별 로그 상세")
  @GetMapping("/menu/result")
  public ApiResponseDto<Page<AuditLogDto.MenuDetail>> getMenuResultLogs(
      @RequestParam String menuId,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size) {
    AuditLogDto.searchReq searchReq = new AuditLogDto.searchReq(page, size, "created_dttm,desc");
    Page<AuditLogDto.MenuDetail> result = auditLogService.getLogByMenuResult(searchReq, menuId);

    return ApiResponseDto.ok(result);
  }

  @Operation(summary = "사용자별 로그 조회")
  @GetMapping("/account")
  public ApiResponseDto<Page<AuditLogDto.UserAuditList>> getAccountLogs(
      @RequestParam(required = false) String searchValue,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size) {
    AuditLogDto.searchReq searchReq = new AuditLogDto.searchReq(page, size, "created_dttm,desc");
    Page<AuditLogDto.UserAuditList> result =
        auditLogService.getLogByAccount(searchReq, searchValue);

    return ApiResponseDto.ok(result);
  }

  @Operation(summary = "사용자별 로그 상세")
  @GetMapping("/account/result")
  public ApiResponseDto<Page<AuditLogDto.UserDetail>> getAccountResultLogs(
      @RequestParam Long userUid,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size) {
    AuditLogDto.searchReq searchReq = new AuditLogDto.searchReq(page, size, "created_dttm,desc");
    Page<AuditLogDto.UserDetail> result = auditLogService.getLogByAccountResult(searchReq, userUid);

    return ApiResponseDto.ok(result);
  }
}
