package com.dbnt.kcscbackend.admin.contents.survey.service;

import com.dbnt.kcscbackend.admin.contents.survey.entity.TnSurvey;
import com.dbnt.kcscbackend.admin.contents.survey.entity.TnSurveyQt;
import com.dbnt.kcscbackend.admin.contents.survey.entity.TnSurveyQtItem;
import com.dbnt.kcscbackend.admin.contents.survey.repository.TnSurveyQtItemRepository;
import com.dbnt.kcscbackend.admin.contents.survey.repository.TnSurveyQtRepository;
import com.dbnt.kcscbackend.admin.contents.survey.repository.TnSurveyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminSurveyService {
  private final TnSurveyRepository surveyRepository;
  private final TnSurveyQtRepository qtRepository;
  private final TnSurveyQtItemRepository itemRepository;

  public List<TnSurvey> selectSurveyList() {
    return surveyRepository.findAllByOrderBySvySeqDesc();
  }

  public TnSurvey selectSurvey(TnSurvey survey) {
    return surveyRepository.findById(survey.getSvySeq()).orElse(null);
  }

  public List<TnSurveyQt> selectSurveyQt(TnSurvey survey) {
    List<TnSurveyQt> qtList = qtRepository.findBySvySeqOrderByQtSeq(survey.getSvySeq());
    List<Integer> qtSeqList = new ArrayList<>();
    for(TnSurveyQt qt: qtList){
      qtSeqList.add(qt.getQtSeq());
    }
    List<TnSurveyQtItem> itemList = itemRepository.findByQtSeqInOrderByQtItemSeq(qtSeqList);
    for(TnSurveyQt qt: qtList){
      for(TnSurveyQtItem item: itemList){
        if(qt.getQtSeq().equals(item.getQtSeq())){
          qt.getItemList().add(item);
        }
      }
    }
    return qtList;
  }

  @Transactional
  public void insertSurvey(TnSurvey survey, String insertUser) {
    if(survey.getSvySeq()==null){
      survey.setFrstCrtId(insertUser);
      survey.setFrstCrtDt(LocalDateTime.now());
      survey.setUseYn("N");
      surveyRepository.save(survey);
    }else{
      TnSurvey savedInfo = surveyRepository.findById(survey.getSvySeq()).orElse(null);
      savedInfo.setSvyTitle(survey.getSvyTitle());
      savedInfo.setSvyDesc(survey.getSvyDesc());
      savedInfo.setSvyStartDt(survey.getSvyStartDt());
      savedInfo.setSvyEndDt(survey.getSvyEndDt());
      savedInfo.setLastChgId(insertUser);
      savedInfo.setLastChgDt(LocalDateTime.now());
      surveyRepository.save(savedInfo);
    }
  }

  @Transactional
  public void updateSurveyUseYn(Integer svySeq, String updateUser) {
    TnSurvey savedInfo = surveyRepository.findById(svySeq).orElse(null);
    savedInfo.setUseYn(savedInfo.getUseYn().equals("Y")?"N":"Y");
    savedInfo.setLastChgId(updateUser);
    savedInfo.setLastChgDt(LocalDateTime.now());
  }

  @Transactional
  public void deleteSurvey(Integer svySeq) {
    surveyRepository.deleteById(svySeq);
  }

  @Transactional
  public void insertSurveyQt(List<TnSurveyQt> qtList, String id) {
    qtRepository.deleteBySvySeq(qtList.get(0).getSvySeq());
    for(TnSurveyQt qt: qtList){
      qt.setQtSeq(null);
    }
    qtRepository.saveAll(qtList);
    for(TnSurveyQt qt: qtList){
      for(TnSurveyQtItem item: qt.getItemList()){
        item.setQtSeq(qt.getQtSeq());
      }
      itemRepository.saveAll(qt.getItemList());
    }
    System.out.println();
  }
}
