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

import com.kamco.cd.training.postgres.repository.train.ModelTestMetricsJobRepository;
import com.kamco.cd.training.train.dto.ModelTrainMetricsDto.ModelMetricJsonDto;
import com.kamco.cd.training.train.dto.ModelTrainMetricsDto.ModelTestFileName;
import com.kamco.cd.training.train.dto.ModelTrainMetricsDto.ResponsePathDto;
import com.kamco.cd.training.train.dto.ModelTrainMetricsDto.SimpleMetricJsonDto;
import java.time.ZonedDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ModelTestMetricsJobCoreService {

  private final ModelTestMetricsJobRepository modelTestMetricsJobRepository;

  @Transactional
  public void updateModelMetricsTrainSaveYn(Long modelId, String stepNo) {
    modelTestMetricsJobRepository.updateModelMetricsTrainSaveYn(modelId, stepNo);
  }

  // Test 로직 시작
  public List<ResponsePathDto> getTestMetricSaveNotYetModelIds() {
    return modelTestMetricsJobRepository.getTestMetricSaveNotYetModelIds();
  }

  public ResponsePathDto getTestMetricSaveNotYetModelId(Long modelId) {
    return modelTestMetricsJobRepository.getTestMetricSaveNotYetModelId(modelId);
  }

  public void insertModelMetricsTest(List<Object[]> batchArgs) {
    modelTestMetricsJobRepository.insertModelMetricsTest(batchArgs);
  }

  public ModelMetricJsonDto getTestMetricPackingInfo(Long modelId) {
    return modelTestMetricsJobRepository.getTestMetricPackingInfo(modelId);
  }

  public SimpleMetricJsonDto getSimpleTestMetricPackingInfo(Long modelId) {
    return modelTestMetricsJobRepository.getSimpleTestMetricPackingInfo(modelId);
  }

  public ModelTestFileName findModelTestFileNames(Long modelId) {
    return modelTestMetricsJobRepository.findModelTestFileNames(modelId);
  }

  @Transactional
  public void updatePackingStart(Long modelId, ZonedDateTime now) {
    modelTestMetricsJobRepository.updatePackingStart(modelId, now);
  }

  @Transactional
  public void updatePackingEnd(Long modelId, ZonedDateTime now, String failSuccState) {
    modelTestMetricsJobRepository.updatePackingEnd(modelId, now, failSuccState);
  }

  /**
   * 특정 Epoch의 메트릭 정보 조회 (하이퍼파라미터별 ZIP 생성용)
   *
   * @param modelId 모델 ID
   * @param epoch Epoch 번호
   * @param metricType 메트릭 타입 (fscore, precision, recall)
   * @return 메트릭 JSON DTO
   */
  public ModelMetricJsonDto getMetricsByEpoch(Long modelId, Integer epoch, String metricType) {
    return modelTestMetricsJobRepository.getMetricsByEpoch(modelId, epoch, metricType);
  }
}
