package com.kamco.cd.training.postgres.core;

import com.kamco.cd.training.common.exception.BadRequestException;
import com.kamco.cd.training.common.exception.NotFoundException;
import com.kamco.cd.training.common.utils.UserUtil;
import com.kamco.cd.training.model.dto.ModelConfigDto;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.DetailSummary;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.HyperSummary;
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.TransferHyperSummary;
import com.kamco.cd.training.model.dto.ModelTrainMngDto.Basic;
import com.kamco.cd.training.model.dto.ModelTrainMngDto.ModelProgressStepDto;
import com.kamco.cd.training.postgres.entity.ModelMasterEntity;
import com.kamco.cd.training.postgres.repository.model.ModelConfigRepository;
import com.kamco.cd.training.postgres.repository.model.ModelDetailRepository;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ModelTrainDetailCoreService {
  private final ModelDetailRepository modelDetailRepository;
  private final ModelConfigRepository modelConfigRepository;
  private final UserUtil userUtil;

  /**
   * UUID로 모델 조회
   *
   * @param uuid UUID
   * @return 모델 Entity
   */
  public ModelMasterEntity findByUuid(String uuid) {
    try {
      UUID uuidObj = UUID.fromString(uuid);
      return modelDetailRepository
          .findByUuid(uuidObj)
          .orElseThrow(() -> new NotFoundException("모델을 찾을 수 없습니다. UUID: " + uuid));
    } catch (IllegalArgumentException e) {
      throw new BadRequestException("잘못된 UUID 형식입니다: " + uuid);
    }
  }

  /**
   * 상세정보 페이지 > 요약정보
   *
   * @param uuid
   * @return
   */
  public DetailSummary getModelDetailSummary(UUID uuid) {
    return modelDetailRepository.getModelDetailSummary(uuid);
  }

  /**
   * 하이퍼 파리미터 요약정보
   *
   * @param uuid 모델마스터 uuid
   * @return
   */
  public HyperSummary getByModelHyperParamSummary(UUID uuid) {
    return modelDetailRepository.getByModelHyperParamSummary(uuid);
  }

  // TODO 미사용시작
  public TransferHyperSummary getTransferHyperSummary(UUID uuid) {
    return modelDetailRepository.getByModelTransferHyperParamSummary(uuid);
  }

  // TODO 미사용 끝

  public List<MappingDataset> getByModelMappingDataset(UUID uuid) {
    return modelDetailRepository.getByModelMappingDataset(uuid);
  }

  public Basic findByModelByUUID(UUID uuid) {
    ModelMasterEntity entity = modelDetailRepository.findByModelByUUID(uuid);
    return entity.toDto();
  }

  // TODO 미사용시작
  /**
   * 모델 학습별 config 정보 조회
   *
   * @param modelId
   * @return
   */
  public ModelConfigDto.Basic findModelConfig(Long modelId) {
    return modelConfigRepository.findModelConfigByModelId(modelId).orElse(null);
  }

  // TODO 미사용 끝
  public List<ModelTrainMetrics> getModelTrainMetricResult(UUID uuid) {
    return modelDetailRepository.getModelTrainMetricResult(uuid);
  }

  public List<ModelValidationMetrics> getModelValidationMetricResult(UUID uuid) {
    return modelDetailRepository.getModelValidationMetricResult(uuid);
  }

  public List<ModelTestMetrics> getModelTestMetricResult(UUID uuid) {
    return modelDetailRepository.getModelTestMetricResult(uuid);
  }

  public ModelBestEpoch getModelTrainBestEpoch(UUID uuid) {
    return modelDetailRepository.getModelTrainBestEpoch(uuid);
  }

  public ModelFileInfo getModelTrainFileInfo(UUID uuid) {
    return modelDetailRepository.getModelTrainFileInfo(uuid);
  }

  public List<ModelProgressStepDto> findModelTrainProgressInfo(UUID uuid) {
    return modelDetailRepository.findModelTrainProgressInfo(uuid);
  }

  public Basic findByModelBeforeId(Long beforeModelId) {
    ModelMasterEntity entity = modelDetailRepository.findByModelBeforeId(beforeModelId);
    return entity.toDto();
  }
}
