// TODO 미사용시작
package com.kamco.cd.training.postgres.core;

import com.kamco.cd.training.common.exception.NotFoundException;
import com.kamco.cd.training.common.service.BaseCoreService;
import com.kamco.cd.training.dataset.dto.MapSheetDto;
import com.kamco.cd.training.postgres.entity.MapSheetEntity;
import com.kamco.cd.training.postgres.repository.dataset.MapSheetRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MapSheetCoreService
    implements BaseCoreService<MapSheetDto.Basic, Long, MapSheetDto.SearchReq> {

  private final MapSheetRepository mapSheetRepository;

  @Override
  public void remove(Long id) {
    Optional<MapSheetEntity> mapSheet = mapSheetRepository.findById(id);
    if (mapSheet.isEmpty()) {
      return;
    }

    MapSheetEntity entity = mapSheet.get();
    entity.setDeleted(true);
    mapSheetRepository.save(entity);
  }

  @Override
  public MapSheetDto.Basic getOneById(Long id) {
    MapSheetEntity entity =
        mapSheetRepository
            .findById(id)
            .orElseThrow(() -> new NotFoundException("도엽을 찾을 수 없습니다. ID: " + id));

    if (entity.getDeleted()) {
      throw new NotFoundException("삭제된 도엽입니다. ID: " + id);
    }

    return entity.toDto();
  }

  @Override
  public Page<MapSheetDto.Basic> search(MapSheetDto.SearchReq searchReq) {
    Page<MapSheetEntity> entityPage = mapSheetRepository.findMapSheetList(searchReq);
    return entityPage.map(MapSheetEntity::toDto);
  }

  /**
   * 도엽 목록 조회
   *
   * @param searchReq 검색 조건
   * @return 페이징 처리된 도엽 목록
   */
  public Page<MapSheetDto.Basic> findMapSheetList(MapSheetDto.SearchReq searchReq) {
    return search(searchReq);
  }

  /**
   * 도엽 삭제 (다건)
   *
   * @param deleteReq 삭제 요청
   */
  public void deleteMapSheets(MapSheetDto.DeleteReq deleteReq) {
    for (Long id : deleteReq.getItemIds()) {
      remove(id);
    }
  }
}
// TODO 미사용 끝
