package com.kamco.cd.training.postgres.repository.model;

import static com.kamco.cd.training.postgres.entity.QModelConfigEntity.modelConfigEntity;
import static com.kamco.cd.training.postgres.entity.QModelMasterEntity.modelMasterEntity;

import com.kamco.cd.training.model.dto.ModelConfigDto.Basic;
import com.kamco.cd.training.model.dto.ModelConfigDto.TransferBasic;
import com.kamco.cd.training.postgres.entity.QModelConfigEntity;
import com.kamco.cd.training.postgres.entity.QModelMasterEntity;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class ModelConfigRepositoryImpl implements ModelConfigRepositoryCustom {

  private final JPAQueryFactory queryFactory;

  @Override
  public Optional<Basic> findModelConfigByModelId(Long modelId) {

    return Optional.ofNullable(
        queryFactory
            .select(
                Projections.constructor(
                    Basic.class,
                    modelConfigEntity.id,
                    modelConfigEntity.model.id,
                    modelConfigEntity.epochCount,
                    modelConfigEntity.trainPercent,
                    modelConfigEntity.validationPercent,
                    modelConfigEntity.testPercent,
                    modelConfigEntity.memo))
            .from(modelConfigEntity)
            .where(modelConfigEntity.model.id.eq(modelId))
            .fetchOne());
  }

  // TODO 미사용시작
  @Override
  public Optional<TransferBasic> findModelTransferConfigByModelId(Long modelId) {
    QModelConfigEntity beforeConfig = new QModelConfigEntity("beforeConfig");
    QModelMasterEntity beforeMaster = new QModelMasterEntity("beforeMaster");

    return Optional.ofNullable(
        queryFactory
            .select(
                Projections.constructor(
                    TransferBasic.class,

                    // ===== 현재 =====
                    modelConfigEntity.id,
                    modelConfigEntity.model.id,
                    modelConfigEntity.epochCount,
                    modelConfigEntity.trainPercent,
                    modelConfigEntity.validationPercent,
                    modelConfigEntity.testPercent,
                    modelConfigEntity.memo,

                    // ===== before =====
                    beforeConfig.id,
                    beforeConfig.model.id,
                    beforeConfig.epochCount,
                    beforeConfig.trainPercent,
                    beforeConfig.validationPercent,
                    beforeConfig.testPercent,
                    beforeConfig.memo))
            .from(modelConfigEntity)
            .innerJoin(modelConfigEntity.model, modelMasterEntity)

            // before 모델 조인
            .leftJoin(beforeMaster)
            .on(beforeMaster.id.eq(modelMasterEntity.beforeModelId))
            .leftJoin(beforeConfig)
            .on(beforeConfig.model.id.eq(beforeMaster.id))
            .where(modelMasterEntity.id.eq(modelId))
            .fetchOne());
  }
  // TODO 미사용 끝
}
