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

import com.kamco.cd.kamcoback.postgres.entity.MapSheetMngFileEntity;
import com.kamco.cd.kamcoback.postgres.repository.scheduler.MapSheetMngFileJobRepository;
import com.kamco.cd.kamcoback.scheduler.dto.MapSheetMngDto;
import com.kamco.cd.kamcoback.scheduler.dto.MapSheetMngDto.MngHstDto;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MapSheetMngFileJobCoreService {

  private final MapSheetMngFileJobRepository mapSheetMngFileJobRepository;

  public Page<MapSheetMngDto.MngDto> findMapSheetMngList(
      MapSheetMngDto.@Valid MngSearchReq searchReq) {
    return mapSheetMngFileJobRepository.findMapSheetMngList(searchReq);
  }

  public List<MngHstDto> findTargetMapSheetFileList(long targetNum, int pageSize) {
    return mapSheetMngFileJobRepository.findTargetMapSheetFileList(targetNum, pageSize);
  }

  public MapSheetMngDto.DmlReturn mngHstDataSyncStateUpdate(
      @Valid MapSheetMngDto.MngHstDto updateReq) {

    mapSheetMngFileJobRepository.mngHstDataSyncStateUpdate(updateReq);

    return new MapSheetMngDto.DmlReturn("success", updateReq.getHstUid() + "");
  }

  public MapSheetMngDto.DmlReturn mngFileSave(@Valid MapSheetMngDto.MngFileAddReq addReq) {

    MapSheetMngFileEntity entity = new MapSheetMngFileEntity();
    entity.setMngYyyy(addReq.getMngYyyy());
    entity.setMapSheetNum(addReq.getMapSheetNum());
    entity.setRefMapSheetNum(addReq.getRefMapSheetNum());
    entity.setFilePath(addReq.getFilePath());
    entity.setFileName(addReq.getFileName());
    entity.setFileExt(addReq.getFileExt());
    entity.setHstUid(addReq.getHstUid());
    entity.setFileSize(addReq.getFileSize());
    entity.setFileState(addReq.getFileState());

    MapSheetMngFileEntity saved = mapSheetMngFileJobRepository.save(entity);
    // int hstCnt = mapSheetMngRepository.insertMapSheetOrgDataToMapSheetMngHst(saved.getMngYyyy());

    return new MapSheetMngDto.DmlReturn("success", saved.getFileUid().toString());
  }
}
