package com.dbnt.faisp.fpiMgt.affair.service;


import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.fpiMgt.affair.mapper.AffairMapper;
import com.dbnt.faisp.fpiMgt.affair.model.AffairBoard;
import com.dbnt.faisp.fpiMgt.affair.model.AffairFile;
import com.dbnt.faisp.fpiMgt.affair.repository.*;
import com.dbnt.faisp.fpiMgt.monthPlan.model.PlanFile;
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 AffairService extends BaseService { // 견문보고
  private final AffairBoardRepository affairBoardRepository;
  private final AffairFileRepository affairFileRepository;
  private final AffairRatingRepository affairRatingRepository;
  private final HashTagRepository hashTagRepository;
  private final HashTagLinkRepository hashTagLinkRepository;
  private final AffairMapper affairMapper;

  public List<AffairBoard> selectAffairBoardList(AffairBoard affair){
    return affairMapper.selectAffairBoardList(affair);
  }
  public Integer selectAffairBoardCnt(AffairBoard affair){
    return affairMapper.selectAffairBoardCnt(affair);
  }

  public AffairBoard selectAffairBoard(Integer affairKey){
    AffairBoard affair = affairBoardRepository.findById(affairKey).orElse(null);
    if(affair != null){
      affair.setFileList(affairFileRepository.findByAffairKey(affairKey));
      affair.setRateList(affairRatingRepository.findByAffairKey(affairKey));
    }
    return affair;
  }
  @Transactional
  public Integer saveAffairBoard(AffairBoard affair, List<Integer> deleteFileSeq){
    Integer affairKey = affairBoardRepository.save(affair).getAffairKey();
    if(deleteFileSeq != null && deleteFileSeq.size()>0){
      deleteAffairFile(affairKey, deleteFileSeq);
    }
    if(affair.getMultipartFileList()!=null){
      saveUploadFiles(affairKey, affair.getMultipartFileList());
    }
    return affairKey;
  }

  private void deleteAffairFile(Integer affairKey, List<Integer> deleteFileSeq) {
    List<AffairFile> affairFileList = affairFileRepository.findByAffairKey(affairKey);
    for(AffairFile file: affairFileList){
      if(deleteFileSeq.contains(file.getFileSeq())){
        deleteStoredFile(new File(file.getSavePath(), file.getConvNm()));
        affairFileRepository.delete(file);
      }
    }
  }
  private void saveUploadFiles(Integer affairKey, List<MultipartFile> multipartFileList){
    AffairFile lastFileInfo = affairFileRepository.findTopByAffairKeyOrderByFileSeqDesc(affairKey).orElse(null);
    int fileSeq = lastFileInfo==null?1:(lastFileInfo.getFileSeq()+1);
    for(MultipartFile file : multipartFileList){
      String saveName = UUID.randomUUID().toString();
      String path = locationPath+File.separator+"monthPlan"+File.separator;
      saveFile(file, new File(path+File.separator+saveName));

      String originalFilename = file.getOriginalFilename();
      int extnIdx = originalFilename.lastIndexOf(".");
      AffairFile fileInfo = new AffairFile();
      fileInfo.setAffairKey(affairKey);
      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);
      affairFileRepository.save(fileInfo);
    }
  }
}
