package com.kamco.cd.training.model.service;

import com.kamco.cd.training.dataset.dto.DatasetDto.DatasetReq;
import com.kamco.cd.training.dataset.dto.DatasetDto.SelectDataSet;
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.TransferDetailDto;
import com.kamco.cd.training.model.dto.ModelTrainDetailDto.TransferHyperSummary;
import com.kamco.cd.training.model.dto.ModelTrainMngDto.Basic;
import com.kamco.cd.training.postgres.core.ModelTrainDetailCoreService;
import com.kamco.cd.training.postgres.core.ModelTrainMngCoreService;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class ModelTrainDetailService {

  private final ModelTrainDetailCoreService modelTrainDetailCoreService;
  private final ModelTrainMngCoreService mngCoreService;

  /**
   * 모델 상세정보 요약
   *
   * @param uuid
   * @return
   */
  public DetailSummary getModelDetailSummary(UUID uuid) {
    return modelTrainDetailCoreService.getModelDetailSummary(uuid);
  }

  /**
   * 모델 하이퍼파라미터 요약
   *
   * @param uuid
   * @return
   */
  public HyperSummary getByModelHyperParamSummary(UUID uuid) {
    return modelTrainDetailCoreService.getByModelHyperParamSummary(uuid);
  }

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

  public Basic findByModelByUUID(UUID uuid) {
    return modelTrainDetailCoreService.findByModelByUUID(uuid);
  }

  /**
   * 전이학습 모델선택 정보
   *
   * @param uuid
   * @return
   */
  public TransferDetailDto getTransferDetail(UUID uuid) {
    Basic modelInfo = modelTrainDetailCoreService.findByModelByUUID(uuid);

    // config 정보 조회
    ModelConfigDto.Basic configInfo = mngCoreService.findModelConfigByModelId(uuid);

    // 하이파라미터 정보 조회
    TransferHyperSummary hyperSummary = modelTrainDetailCoreService.getTransferHyperSummary(uuid);
    List<SelectDataSet> dataSets = new ArrayList<>();

    DatasetReq datasetReq = new DatasetReq();
    List<Long> datasetIds = new ArrayList<>();

    List<MappingDataset> mappingDatasets =
        modelTrainDetailCoreService.getByModelMappingDataset(uuid);
    for (MappingDataset mappingDataset : mappingDatasets) {
      datasetIds.add(mappingDataset.getDatasetId());
    }
    datasetReq.setIds(datasetIds);
    datasetReq.setModelNo(modelInfo.getModelNo());

    if (modelInfo.getModelNo().equals("G1")) {
      dataSets = mngCoreService.getDatasetSelectG1List(datasetReq);
    } else {
      dataSets = mngCoreService.getDatasetSelectG2G3List(datasetReq);
    }

    TransferDetailDto transferDetailDto = new TransferDetailDto();
    transferDetailDto.setEtcConfig(configInfo);
    transferDetailDto.setModelTrainHyper(hyperSummary);
    transferDetailDto.setModelTrainDataset(dataSets);

    return transferDetailDto;
  }
}
