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

import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
import com.kamco.cd.kamcoback.model.dto.ModelMngDto;
import com.kamco.cd.kamcoback.postgres.entity.ModelMngEntity;
import com.kamco.cd.kamcoback.postgres.repository.model.ModelMngRepository;
import jakarta.persistence.EntityNotFoundException;
import java.time.LocalDate;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ModelMngCoreService {

  private final ModelMngRepository modelMngRepository;

  public Page<ModelMngDto.ModelList> findModelMgmtList(
      ModelMngDto.searchReq searchReq,
      LocalDate startDate,
      LocalDate endDate,
      String modelType,
      String searchVal) {
    return modelMngRepository.findModelMgmtList(
        searchReq, startDate, endDate, modelType, searchVal);
  }

  public ApiResponseDto.ResponseObj removeModel(String modelVer) {
    ModelMngEntity entity =
        modelMngRepository
            .findByModelUid(modelVer)
            .orElseThrow(() -> new EntityNotFoundException("model을 찾을 수 없습니다. ver: " + modelVer));

    // id 코드 deleted = true 업데이트
    entity.deleted();
    return new ApiResponseDto.ResponseObj(ApiResponseDto.ApiResponseCode.OK, "");
  }
}
