package com.dbnt.faisp.faRpt.service;

import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.config.FileInfo;
import com.dbnt.faisp.faRpt.mapper.FaRptMapper;
import com.dbnt.faisp.faRpt.model.FaRptBoard;
import com.dbnt.faisp.faRpt.model.FaRptFile;
import com.dbnt.faisp.faRpt.model.FaRptReadUser;
import com.dbnt.faisp.faRpt.repository.FaRptBoardRepository;
import com.dbnt.faisp.faRpt.repository.FaRptFileRepository;
import com.dbnt.faisp.faRpt.repository.FaRptReadUserRepository;
import com.dbnt.faisp.publicBoard.model.PublicBoard;
import com.dbnt.faisp.publicBoard.model.PublicComment;
import com.dbnt.faisp.publicBoard.model.PublicFile;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;


@Service
@RequiredArgsConstructor
public class FaRptService extends BaseService {
  private final FaRptBoardRepository faRptBoardRepository;
  private final FaRptFileRepository faRptFileRepository;
  private final FaRptReadUserRepository faRptReadUserRepository;
  private final FaRptMapper faRptMapper;


  public List<FaRptBoard> selectFaRptList(FaRptBoard faRptBoard) {
    return faRptMapper.selectFaRptList(faRptBoard);
  }

  public Integer selectFaRptCnt(FaRptBoard faRptBoard) {
    return faRptMapper.selectFaRptCnt(faRptBoard);
  }

  @Transactional
  public Integer saveFaRptBoard(FaRptBoard faRptBoard, List<Integer> deleteFileSeq) {
    Integer faRptKey = faRptBoardRepository.save(faRptBoard).getFaRptKey();
    if(deleteFileSeq!=null && deleteFileSeq.size()>0){
      deleteFaRptFile(faRptKey, deleteFileSeq);
    }
    if(faRptBoard.getMultipartFileList() != null){
      saveUploadFiles(faRptKey, faRptBoard.getMultipartFileList());
    }
    if(faRptBoard.getReadUserList() != null){
      saveFaRptReadUser(faRptKey, faRptBoard.getReadUserList());
    }
    return faRptKey;
  }

  public FaRptBoard selectFaRptBoard(Integer faRptKey) {
    FaRptBoard faRptBoard = faRptBoardRepository.findById(faRptKey).orElse(null);
    faRptBoard.setFileList(faRptFileRepository.findByFaRptKey(faRptKey));
    faRptBoard.setReadUserList(faRptReadUserRepository.findByFaRptKey(faRptKey));
    return faRptBoard;
  }



  private void saveFaRptReadUser(Integer faRptKey, List<FaRptReadUser> readUserList) {
    faRptReadUserRepository.deleteByFaRptKey(faRptKey);
    for(FaRptReadUser readUser: readUserList){
      readUser.setFaRptKey(faRptKey);
    }
    faRptReadUserRepository.saveAll(readUserList);
  }

  private void saveUploadFiles(Integer faRptKey, List<MultipartFile> multipartFileList) {
    FaRptFile lastFile = faRptFileRepository.findTopByFaRptKeyOrderByFileSeq(faRptKey).orElse(null);
    int fileSeq = lastFile==null?1:(lastFile.getFileSeq()+1);
    for(MultipartFile file: multipartFileList){
      String saveName = UUID.randomUUID().toString();
      String path = locationPath+File.separator+"faRpt"+File.separator;
      saveFile(file, new File(path+File.separator+saveName));

      String originalFilename = file.getOriginalFilename();
      int extnIdx = originalFilename.lastIndexOf(".");
      FaRptFile fileInfo = new FaRptFile();
      fileInfo.setFaRptKey(faRptKey);
      fileInfo.setFileSeq(fileSeq++);
      fileInfo.setOrigNm(originalFilename.substring(0, extnIdx));
      fileInfo.setFileExtn(originalFilename.substring(extnIdx+1));
      fileInfo.setConvNm(saveName);
      fileInfo.setFileSize(calculationSize(file.getSize()));
      fileInfo.setSavePath(path);
      faRptFileRepository.save(fileInfo);
    }
  }

  private void deleteFaRptFile(Integer faRptKey, List<Integer> deleteFileSeq) {
    List<FaRptFile> fileList = faRptFileRepository.findByFaRptKey(faRptKey);
    for(FaRptFile file: fileList){
      if(deleteFileSeq.contains(file.getFileSeq())){
        deleteStoredFile(new File(file.getSavePath(), file.getConvNm()));
        faRptFileRepository.delete(file);
      }
    }
  }
}