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

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

import com.kamco.cd.training.model.dto.ModelConfigDto.Basic;
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());
  }
}
