package com.cd.detection.domain.zoo;

import com.cd.detection.config.api.ApiResponseDto;
import com.cd.detection.domain.zoo.dto.AnimalDto;
import com.cd.detection.domain.zoo.service.AnimalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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 lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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 = "동물", description = "동물 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/animal")
public class AnimalController {

  private final AnimalService animalService;

  @Operation(summary = "동물 목록", description = "동물 목록 ")
  @ApiResponses(
      value = {
          @ApiResponse(
              responseCode = "200",
              description = "검색 성공",
              content =
              @Content(
                  mediaType = "application/json",
                  schema = @Schema(implementation = Page.class))),
          @ApiResponse(responseCode = "400", description = "잘못된 검색 조건", content = @Content),
          @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/list")
  public ApiResponseDto<Page<AnimalDto.Basic>> getZooWithAnimals(
      @Parameter(description = "페이지 번호 (0부터 시작)", example = "0")
      @RequestParam(defaultValue = "0")
      int page,
      @Parameter(description = "페이지 크기", example = "20")
      @RequestParam(defaultValue = "20")
      int size) {

    AnimalDto.SearchListRequest searchListRequest = new AnimalDto.SearchListRequest();
    searchListRequest.setPage(page);
    searchListRequest.setSize(size);
    return ApiResponseDto.ok(animalService.getAnimals(searchListRequest));
  }

  @Operation(summary = "동물 등록", description = "동물 등록 ")
  @ApiResponses(
      value = {
          @ApiResponse(
              responseCode = "200",
              description = "등록 성공",
              content = @Content(
                  mediaType = "text/plain",
                  schema = @Schema(type = "string", example = "success")
              )),
          @ApiResponse(responseCode = "400", description = "잘못된 검색 조건", content = @Content),
          @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PostMapping
  public ApiResponseDto<String> saveAnimal(
      @io.swagger.v3.oas.annotations.parameters.RequestBody(
          content = @Content(
              schema = @Schema(implementation = AnimalDto.AddRequest.class)
          )
      )
      @RequestBody AnimalDto.AddRequest dto) {
    animalService.saveAnimal(dto);
    return ApiResponseDto.ok("success");
  }

  @Operation(summary = "동물 수정", description = "동물 수정 ")
  @ApiResponses(
      value = {
          @ApiResponse(
              responseCode = "200",
              description = "수정 성공",
              content =
              @Content(
                  mediaType = "application/json",
                  schema = @Schema(implementation = String.class))),
          @ApiResponse(responseCode = "400", description = "잘못된 검색 조건", content = @Content),
          @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @PutMapping
  public ApiResponseDto<String> updateAnimal(@RequestBody AnimalDto.ModifyRequest dto) {
    animalService.updateAnimal(dto);
    return ApiResponseDto.ok("success");
  }

  @Operation(summary = "동물 삭제", description = "동물 삭제 ")
  @ApiResponses(
      value = {
          @ApiResponse(
              responseCode = "200",
              description = "삭제 성공",
              content =
              @Content(
                  mediaType = "application/json",
                  schema = @Schema(implementation = String.class))),
          @ApiResponse(responseCode = "400", description = "잘못된 검색 조건", content = @Content),
          @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @DeleteMapping
  public ApiResponseDto<String> deleteAnimal(@RequestBody AnimalDto.ModifyRequest dto) {
    animalService.deleteAnimal(dto);
    return ApiResponseDto.ok("success");
  }


}
