package com.kamco.cd.training.model;

import com.kamco.cd.training.common.download.RangeDownloadResponder;
import com.kamco.cd.training.config.api.ApiResponseDto;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.MappingDataset;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.ModelBestEpoch;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.ModelFileInfo;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.ModelTestMetrics;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.ModelTrainMetrics;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.ModelValidationMetrics;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.TransferDetailDto;
import com.kamco.cd.training.model.dto.ModelTrainMngDto.Basic;
import com.kamco.cd.training.model.dto.ModelTrainMngDto.CleanupResult;
import com.kamco.cd.training.model.dto.ModelTrainMngDto.ModelProgressStepDto;
import com.kamco.cd.training.model.service.ModelTrainDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
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.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.apache.coyote.BadRequestException;
import org.springframework.beans.factory.annotation.Value;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@Tag(name = "모델학습 관리", description = "어드민 홈 > 모델학습관리 > 모델관리 > 목록")
@RequestMapping("/api/models")
public class ModelTrainDetailApiController {
  private final ModelTrainDetailService modelTrainDetailService;
  private final RangeDownloadResponder rangeDownloadResponder;

  @Value("${train.docker.response_dir}")
  private String responseDir;

  @Operation(summary = "모델학습관리> 모델관리 > 상세정보탭 > 학습 진행정보", description = "학습 진행정보, 모델학습 정보 API")
  @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("/detail/{uuid}")
  public ApiResponseDto<Basic> findByModelByUUID(
      @Parameter(description = "모델학습 uuid", example = "7fbdff54-ea87-4b02-90d1-955fa2a3457e")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.findByModelByUUID(uuid));
  }

  @Operation(summary = "모델학습관리> 모델관리 > 상세정보탭 > 요약정보", description = "상세정보 탭 요약정보 API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = ModelTrainDetailDto.DetailSummary.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/summary/{uuid}")
  public ApiResponseDto<ModelTrainDetailDto.DetailSummary> getModelDetailSummary(
      @Parameter(description = "모델학습 uuid", example = "7fbdff54-ea87-4b02-90d1-955fa2a3457e")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getModelDetailSummary(uuid));
  }

  @Operation(summary = "모델학습관리> 모델관리 > 상세정보탭 > 하이퍼파라미터 요약 정보", description = "하이퍼파라미터 요약 정보 API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = ModelTrainDetailDto.HyperSummary.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/hyper-summary/{uuid}")
  public ApiResponseDto<ModelTrainDetailDto.HyperSummary> getByModelHyperParamSummary(
      @Parameter(description = "모델학습 uuid", example = "7fbdff54-ea87-4b02-90d1-955fa2a3457e")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getByModelHyperParamSummary(uuid));
  }

  @Operation(summary = "모델학습관리> 모델관리 > 상세정보탭 > 데이터셋 정보", description = "모델학습 상세 데이터셋 정보 API")
  @ApiResponses({
    @ApiResponse(
        responseCode = "200",
        description = "조회 성공",
        content =
            @Content(
                mediaType = "application/json",
                array = @ArraySchema(schema = @Schema(implementation = MappingDataset.class)))),
    @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음"),
    @ApiResponse(responseCode = "500", description = "서버 오류")
  })
  @GetMapping("/mapp-dataset/{uuid}")
  public ApiResponseDto<List<MappingDataset>> getByModelMappingDataset(
      @Parameter(description = "모델학습 uuid", example = "7fbdff54-ea87-4b02-90d1-955fa2a3457e")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getByModelMappingDataset(uuid));
  }

  //  @Operation(summary = "모델관리 > 전이 학습 실행설정 > 모델선택", description = "모델선택 정보 API")
  //  @ApiResponses(
  //      value = {
  //        @ApiResponse(
  //            responseCode = "200",
  //            description = "조회 성공",
  //            content =
  //                @Content(
  //                    mediaType = "application/json",
  //                    schema = @Schema(implementation = TransferDetailDto.class))),
  //        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
  //        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
  //      })
  //  @GetMapping("/transfer/detail/{uuid}")
  //  public ApiResponseDto<TransferDetailDto> getTransferDetail(
  //      @Parameter(description = "모델 uuid", example = "7fbdff54-ea87-4b02-90d1-955fa2a3457e")
  //          @PathVariable
  //          UUID uuid) {
  //    return ApiResponseDto.ok(modelTrainDetailService.getTransferDetail(uuid));
  //  }

  @Operation(summary = "모델관리 > 모델 상세 > 성능 정보 (Train)", description = "모델 상세 > 성능 정보 (Train) API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TransferDetailDto.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/metrics/train/{uuid}")
  public ApiResponseDto<List<ModelTrainMetrics>> getModelTrainMetricResult(
      @Parameter(description = "모델 uuid", example = "95cb116c-380a-41c0-98d8-4d1142f15bbf")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getModelTrainMetricResult(uuid));
  }

  @Operation(
      summary = "모델관리 > 모델 상세 > 성능 정보 (Validation)",
      description = "모델 상세 > 성능 정보 (Validation) API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TransferDetailDto.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/metrics/validation/{uuid}")
  public ApiResponseDto<List<ModelValidationMetrics>> getModelValidationMetricResult(
      @Parameter(description = "모델 uuid", example = "95cb116c-380a-41c0-98d8-4d1142f15bbf")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getModelValidationMetricResult(uuid));
  }

  @Operation(summary = "모델관리 > 모델 상세 > 성능 정보 (Test)", description = "모델 상세 > 성능 정보 (Test) API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TransferDetailDto.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/metrics/test/{uuid}")
  public ApiResponseDto<List<ModelTestMetrics>> getModelTestMetricResult(
      @Parameter(description = "모델 uuid", example = "95cb116c-380a-41c0-98d8-4d1142f15bbf")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getModelTestMetricResult(uuid));
  }

  @Operation(summary = "모델관리 > 모델 상세 > 성능 정보 (Test)", description = "모델 상세 > 성능 정보 (Test) API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TransferDetailDto.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/best-epoch/{uuid}")
  public ApiResponseDto<ModelBestEpoch> getModelTrainBestEpoch(
      @Parameter(description = "모델 uuid", example = "95cb116c-380a-41c0-98d8-4d1142f15bbf")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getModelTrainBestEpoch(uuid));
  }

  @Operation(
      summary = "학습데이터 파일 다운로드",
      description = "학습데이터 파일 다운로드",
      parameters = {
        @Parameter(
            name = "kamco-download-uuid",
            in = ParameterIn.HEADER,
            required = true,
            description = "다운로드 요청 UUID",
            schema =
                @Schema(
                    type = "string",
                    format = "uuid",
                    example = "6d8d49dc-0c9d-4124-adc7-b9ca610cc394"))
      })
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "학습데이터 zip파일 다운로드",
            content =
                @Content(
                    mediaType = "application/octet-stream",
                    schema = @Schema(type = "string", format = "binary"))),
        @ApiResponse(responseCode = "404", description = "파일 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/download/{uuid}")
  public ResponseEntity<?> download(@PathVariable UUID uuid, HttpServletRequest request)
      throws IOException {

    Basic info = modelTrainDetailService.findByModelByUUID(uuid);
    Path zipPath =
        Paths.get(responseDir)
            .resolve(String.valueOf(info.getUuid()))
            .resolve(info.getModelVer() + ".zip");

    if (!Files.isRegularFile(zipPath)) {
      throw new BadRequestException();
    }

    return rangeDownloadResponder.buildZipResponse(zipPath, info.getModelVer() + ".zip", request);
  }

  @Operation(summary = "모델관리 > 모델 상세 > 파일 정보", description = "모델 상세 > 파일 정보 API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TransferDetailDto.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/file-info/{uuid}")
  public ApiResponseDto<ModelFileInfo> getModelTrainFileInfo(
      @Parameter(description = "모델 uuid", example = "95cb116c-380a-41c0-98d8-4d1142f15bbf")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.getModelTrainFileInfo(uuid));
  }

  @Operation(summary = "모델관리 > 모델별 진행 상황", description = "모델관리 > 모델별 진행 상황 API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = ModelProgressStepDto.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/progress/{uuid}")
  public ApiResponseDto<List<ModelProgressStepDto>> findModelTrainProgressInfo(
      @Parameter(description = "모델 uuid", example = "95cb116c-380a-41c0-98d8-4d1142f15bbf")
          @PathVariable
          UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.findModelTrainProgressInfo(uuid));
  }

  @Operation(
      summary = "모델관리 > 모델 상세 > best epoch 제외 삭제 될 파일 미리보기",
      description = "best epoch 제외 삭제 될 파일 미리보기 API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = CleanupResult.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/{uuid}/cleanup/preview")
  public ApiResponseDto<CleanupResult> previewCleanup(
      @Parameter(description = "모델 uuid") @PathVariable UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.previewCleanup(uuid));
  }

  @Operation(summary = "모델관리 > 모델 상세 > best epoch 제외 삭제", description = "best epoch 제외 파일 삭제 API")
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "삭제 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = CleanupResult.class))),
        @ApiResponse(responseCode = "404", description = "데이터셋을 찾을 수 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @DeleteMapping("/{uuid}/cleanup")
  public ApiResponseDto<CleanupResult> cleanup(
      @Parameter(description = "모델 uuid") @PathVariable UUID uuid) {
    return ApiResponseDto.ok(modelTrainDetailService.cleanup(uuid));
  }

  @Operation(
      summary = "학습 결과 ZIP 파일 목록 조회",
      description = "모델 UUID에 해당하는 모든 ZIP 파일 목록과 개별 다운로드 링크 반환",
      parameters = {
        @Parameter(
            name = "kamco-download-uuid",
            in = ParameterIn.HEADER,
            required = true,
            description = "다운로드 요청 UUID",
            schema =
                @Schema(
                    type = "string",
                    format = "uuid",
                    example = "6d8d49dc-0c9d-4124-adc7-b9ca610cc394"))
      })
  @ApiResponses(
      value = {
        @ApiResponse(
            responseCode = "200",
            description = "ZIP 파일 목록 조회 성공",
            content =
                @Content(
                    mediaType = "application/json",
                    schema =
                        @Schema(implementation = ModelTrainDetailDto.ZipFileListResponse.class))),
        @ApiResponse(responseCode = "404", description = "모델 또는 파일 없음", content = @Content),
        @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
      })
  @GetMapping("/downloadzip/{uuid}")
  public ResponseEntity<?> downloadZip(
      @Parameter(description = "모델 UUID") @PathVariable UUID uuid,
      @Parameter(hidden = true) @RequestHeader("kamco-download-uuid") String downloadUuid,
      HttpServletRequest request)
      throws IOException {

    return ResponseEntity.ok(
        modelTrainDetailService.getZipFileListWithFullUrl(uuid, downloadUuid, request));
  }
}
