package com.kamco.cd.kamcoback.Innopam;

import com.kamco.cd.kamcoback.Innopam.dto.DetectMastDto;
import com.kamco.cd.kamcoback.Innopam.dto.DetectMastDto.Basic;
import com.kamco.cd.kamcoback.Innopam.dto.DetectMastDto.DetectMastReq;
import com.kamco.cd.kamcoback.Innopam.dto.DetectMastDto.DetectMastSearch;
import com.kamco.cd.kamcoback.Innopam.dto.DetectMastDto.FeaturePnuDto;
import com.kamco.cd.kamcoback.Innopam.service.DetectMastService;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "이노펨 mockup API", description = "이노펨 mockup API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/kcd/cdi/detect")
public class InnopamApiController {

  private final DetectMastService detectMastService;

  /** 탐지결과 등록 */
  @Operation(summary = "탐지결과 등록", description = "탐지결과 등록")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "201",
            description = "등록 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = DetectMastReq.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping("/mast/regist")
  public DetectMastReq setChangeDetection(
      @RequestBody @Valid DetectMastDto.DetectMastReq detectMast) {
    detectMastService.saveDetectMast(detectMast);
    return detectMast;
  }

  @Operation(summary = "탐지결과 삭제", description = "탐지결과 삭제")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "201",
            description = "등록 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = DetectMastReq.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping("/mast/remove")
  public String deleteChangeDetection(@RequestBody DetectMastReq detectMast) {
    return "OK";
  }

  @Operation(summary = "탐지결과 등록목록 조회", description = "탐지결과 등록목록 조회")
  @GetMapping("/mast/list")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "목록 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Basic.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  public List<Basic> selectChangeDetectionList(
      @RequestParam(required = false) String cprsBfYr,
      @RequestParam(required = false) String cprsAdYr,
      @RequestParam(required = false) Integer dtctSno) {
    DetectMastSearch detectMastSearch = new DetectMastSearch();
    detectMastSearch.setCprsAdYr(cprsAdYr);
    detectMastSearch.setCprsBfYr(cprsBfYr);
    detectMastSearch.setDtctSno(dtctSno);
    return detectMastService.selectDetectMast(detectMastSearch);
  }

  @Operation(summary = "탐지결과 등록목록 상세 조회", description = "탐지결과 등록목록 상세 조회")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "목록 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Basic.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/mast/list/{dtctMstId}")
  public Basic selectChangeDetectionDetail(@PathVariable Long dtctMstId) {
    return detectMastService.selectDetectMast(dtctMstId);
  }

  @Operation(summary = "탐지객체 PNU 리스트 조회", description = "탐지객체 PNU 리스트 조회")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "목록 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Basic.class))),
        @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/pnu/{cprsBfYr}/{cprsAfYr}/{dtctSno}")
  public List<FeaturePnuDto> selectPnuList(
      @PathVariable String cprsBfYr, @PathVariable String cprsAfYr, @PathVariable String dtctSno) {
    DetectMastSearch detectMastSearch = new DetectMastSearch();
    detectMastSearch.setCprsAdYr(cprsAfYr);
    detectMastSearch.setCprsBfYr(cprsBfYr);
    detectMastSearch.setDtctSno(Integer.parseInt(dtctSno));
    return detectMastService.findPnuData(detectMastSearch);
  }

  @GetMapping("/pnu/{cprsBfYr}/{cprsAfYr}/{dtctSno}/{featureId}")
  public FeaturePnuDto selectPnuDetail(
      @PathVariable String cprsBfYr,
      @PathVariable String cprsAfYr,
      @PathVariable String dtctSno,
      @PathVariable String featureId) {
    DetectMastSearch detectMastSearch = new DetectMastSearch();
    detectMastSearch.setCprsAdYr(cprsAfYr);
    detectMastSearch.setCprsBfYr(cprsBfYr);
    detectMastSearch.setDtctSno(Integer.parseInt(dtctSno));
    detectMastSearch.setFeatureId(featureId);
    return new FeaturePnuDto();
  }
}
