package com.kamco.cd.kamcoback.model;

import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
import com.kamco.cd.kamcoback.model.dto.ModelMngDto;
import com.kamco.cd.kamcoback.model.dto.ModelVerDto;
import com.kamco.cd.kamcoback.model.service.ModelMngService;
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.transaction.Transactional;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Tag(name = "모델 관리", description = "모델 관리 API")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/model/deprecated")
@Transactional
@Deprecated
public class ModelMngApiController {

  private final ModelMngService modelMngService;

  @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)
      })
  @GetMapping
  public ApiResponseDto<List<ModelMngDto.Basic>> getFindAll() {
    return ApiResponseDto.ok(modelMngService.findModelMngAll());
  }

  /**
   * 최종 등록 모델 정보
   *
   * @return ModelMngDto.FinalModelDto
   */
  @Operation(summary = "최종 등록 모델 조회", description = "최종 등록 모델 조회")
  @GetMapping("/final-model-info")
  public ApiResponseDto<Optional<ModelMngDto.FinalModelDto>> getFinalModelInfo() {
    return ApiResponseDto.ok(modelMngService.getFinalModelInfo());
  }

  /**
   * 모델 등록 => 모델, 버전 동시 등록 (UI 상 따로 등록하는 곳 없음)
   *
   * @param addReq 모델 입력 값
   * @return ModelVerDto.Basic
   */
  @Operation(summary = "모델 등록", description = "모델 등록")
  @PostMapping
  public ApiResponseDto<ModelVerDto.Basic> save(@RequestBody ModelMngDto.AddReq addReq) {
    return ApiResponseDto.createOK(modelMngService.save(addReq));
  }

  @Operation(summary = "모델 수정", description = "모델 수정")
  @PutMapping("/{id}")
  public ApiResponseDto<Long> update(
      @PathVariable Long id, @RequestBody ModelMngDto.AddReq addReq) {
    return ApiResponseDto.ok(modelMngService.update(id, addReq));
  }

  @Operation(summary = "모델 삭제", description = "모델 삭제")
  @DeleteMapping("/{id}")
  public ApiResponseDto<Long> delete(@PathVariable Long id) {
    return ApiResponseDto.deleteOk(modelMngService.delete(id));
  }

  @Operation(summary = "모델 등록 이력", description = "모델 등록 이력")
  @GetMapping("/reg-history")
  public ApiResponseDto<Page<ModelMngDto.ModelRegHistory>> getRegHistoryList(
      @RequestParam(required = false) LocalDate startDate,
      @RequestParam(required = false) LocalDate endDate,
      @RequestParam int page,
      @RequestParam(defaultValue = "20") int size,
      @RequestParam(required = false) String searchVal,
      @RequestParam(required = false) String searchColumn) {
    ModelMngDto.searchReq searchReq =
        new ModelMngDto.searchReq(
            page, size, Optional.ofNullable(searchColumn).orElse("createdDate") + ",desc");
    // searchColumn:: Entity 컬럼명칭으로 -> 기본값 = 등록일 createdDate, (선택) 배포일 deployDttm

    Page<ModelMngDto.ModelRegHistory> result =
        modelMngService.getRegHistoryList(searchReq, startDate, endDate, searchVal);

    return ApiResponseDto.ok(result);
  }
}
