package com.kamco.cd.kamcoback.mapsheet;

import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
import com.kamco.cd.kamcoback.code.service.CommonCodeService;
import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
import com.kamco.cd.kamcoback.mapsheet.dto.FileDto.FilesDto;
import com.kamco.cd.kamcoback.mapsheet.dto.FileDto.FoldersDto;
import com.kamco.cd.kamcoback.mapsheet.dto.FileDto.SrchFilesDepthDto;
import com.kamco.cd.kamcoback.mapsheet.dto.FileDto.SrchFilesDto;
import com.kamco.cd.kamcoback.mapsheet.dto.FileDto.SrchFoldersDto;
import com.kamco.cd.kamcoback.mapsheet.dto.ImageryDto;
import com.kamco.cd.kamcoback.mapsheet.dto.MapSheetMngDto;
import com.kamco.cd.kamcoback.mapsheet.service.MapSheetMngService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
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 jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Tag(name = "영상 관리", description = "영상 관리 API")
@RestController
@RequiredArgsConstructor
@RequestMapping({"/api/mapsheet"})
public class MapSheetMngApiController {

  private final CommonCodeService commonCodeService;
  private final MapSheetMngService mapSheetMngService;

  @Operation(summary = "폴더 조회", description = "폴더 조회")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = CommonCodeDto.Basic.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping("/folder-list")
  public ApiResponseDto<FoldersDto> getDir(@RequestBody SrchFoldersDto srchDto) {
    return ApiResponseDto.createOK(mapSheetMngService.getFolderAll(srchDto));
  }

  @Operation(summary = "지정폴더내 파일목록 조회", description = "지정폴더내 파일목록 조회")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = CommonCodeDto.Basic.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping("/file-list")
  public ApiResponseDto<FilesDto> getFiles(@RequestBody SrchFilesDto srchDto) {

    return ApiResponseDto.createOK(mapSheetMngService.getFilesAll(srchDto));
  }

  @Operation(summary = "지정폴더(하위폴더포함) 파일목록 조회", description = "지정폴더(하위폴더포함) 파일목록 조회")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = CommonCodeDto.Basic.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping("/file-all-list")
  public ApiResponseDto<FilesDto> getAllFiles(@RequestBody SrchFilesDepthDto srchDto) {

    return ApiResponseDto.createOK(mapSheetMngService.getFilesDepthAll(srchDto));
  }

  /**
   * 오류데이터 목록 조회
   *
   * @param searchReq
   * @return
   */
  @PostMapping("/error-list")
  public ApiResponseDto<Page<MapSheetMngDto.ErrorDataDto>> findMapSheetErrorList(
      @RequestBody @Valid MapSheetMngDto.searchReq searchReq) {
    return ApiResponseDto.ok(mapSheetMngService.findMapSheetErrorList(searchReq));
  }

  @Operation(summary = "영상데이터관리목록 조회", description = "영상데이터관리목록 조회")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = CommonCodeDto.Basic.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping("/mng-list")
  public ApiResponseDto<Page<MapSheetMngDto.MngDto>> findMapSheetMngList(
      @RequestBody @Valid MapSheetMngDto.searchReq searchReq) {
    return ApiResponseDto.ok(mapSheetMngService.findMapSheetMngList(searchReq));
  }


  @Operation(summary = "영상데이터관리 > 영상파일 동기화", description = "영상파일 동기화")
  @ApiResponses(
    value = {
      @ApiResponse(
        responseCode = "200",
        description = "동기화 성공",
        content =
        @Content(
          mediaType = "application/json",
          schema = @Schema(implementation = CommonCodeDto.Basic.class))),
      @ApiResponse(responseCode = "404", description = "동기화 할수 없음", content = @Content),
      @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
    })
  @PostMapping("/sync-process")
  public ApiResponseDto<MapSheetMngDto.DmlReturn> uploadProcess(
    @RequestBody @Valid ImageryDto.searchReq searchReq) {
    return ApiResponseDto.ok(mapSheetMngService.syncProcess(searchReq));
  }


  /**
   * @param hstUidList
   * @return
   */
  @Operation(summary = "오류데이터 팝업 > 업로드 처리", description = "오류데이터 팝업 > 업로드 처리")
  @PutMapping("/upload-process")
  public ApiResponseDto<MapSheetMngDto.DmlReturn> uploadProcess(
      @RequestBody @Valid List<Long> hstUidList) {
    return ApiResponseDto.ok(mapSheetMngService.uploadProcess(hstUidList));
  }

  /**
   * @param hstUidList
   * @return
   */
  @Operation(summary = "오류데이터 팝업 > 추론 제외", description = "오류데이터 팝업 > 추론 제외")
  @PutMapping("/except-inference")
  public ApiResponseDto<MapSheetMngDto.DmlReturn> updateExceptUseInference(
      @RequestBody @Valid List<Long> hstUidList) {
    return ApiResponseDto.ok(mapSheetMngService.updateExceptUseInference(hstUidList));
  }
}
