package com.dbnt.faisp.main.userInfo.service;

import com.dbnt.faisp.main.userInfo.mapper.UserInfoMapper;
import com.dbnt.faisp.main.userInfo.model.PersonnelStatus;
import com.dbnt.faisp.main.userInfo.model.UserCareer;
import com.dbnt.faisp.main.userInfo.model.UserEdu;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.userInfo.repository.PersonnelStatusRepository;
import com.dbnt.faisp.main.userInfo.repository.UserCareerRepository;
import com.dbnt.faisp.main.userInfo.repository.UserEduRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PoliceService {

  private final UserCareerRepository userCareerRepository;
  private final UserEduRepository userEduRepository;
  private final PersonnelStatusRepository personnelStatusRepository;
  private final UserInfoMapper userInfoMapper;


  public void saveCareer(UserCareer career) {
    UserCareer lastCareer = userCareerRepository.findTop1ByUserSeqOrderByCareerSeqDesc(career.getUserSeq()).orElse(null);
    career.setCareerSeq(lastCareer==null?1:(lastCareer.getCareerSeq()+1));
    career.setWorkMonth((int) ChronoUnit.MONTHS.between(career.getStartDate(), career.getEndDate()));
    career.setWorkDay((int) ChronoUnit.DAYS.between(career.getStartDate(), career.getEndDate()));
    userCareerRepository.save(career);
  }

  public void saveCareerList(List<UserCareer> careerList) {
    UserCareer lastCareer = userCareerRepository.findTop1ByUserSeqOrderByCareerSeqDesc(careerList.get(0).getUserSeq()).orElse(null);
    int careerSeq = lastCareer==null?1:(lastCareer.getCareerSeq()+1);
    for(UserCareer career: careerList){
      career.setCareerSeq(careerSeq++);
    }
    userCareerRepository.saveAll(careerList);
  }

  public List<UserCareer> selectCareerList(Integer userSeq) {
    return userCareerRepository.findByUserSeqOrderByStartDateDesc(userSeq);
  }

  public void deleteCareer(UserCareer career) {
    userCareerRepository.deleteById(new UserCareer.UserCareerId(career.getUserSeq(), career.getCareerSeq()));
  }

  @Transactional
  public int saveEdu(@AuthenticationPrincipal UserInfo loginUser, List<UserEdu> userEdu) {
    int userSeq = 0;
    for(UserEdu ue : userEdu ) {
      UserEdu dbEdu = userEduRepository.findTopByUserSeqOrderByEduSeqDesc(ue.getUserSeq());
      if (dbEdu == null) {
        ue.setEduSeq(1);
        ue.setWrtOrgan(loginUser.getOgCd());
        ue.setWrtPart(loginUser.getOfcCd());
        ue.setWrtTitle(loginUser.getTitleCd());
        ue.setWrtUserSeq(loginUser.getUserSeq());
        ue.setWrtNm(loginUser.getUserNm());
        ue.setWrtDt(LocalDateTime.now());
        userEduRepository.save(ue);
      } else {
        ue.setEduSeq(dbEdu.getEduSeq()+ 1);
        ue.setWrtOrgan(loginUser.getOgCd());
        ue.setWrtPart(loginUser.getOfcCd());
        ue.setWrtTitle(loginUser.getTitleCd());
        ue.setWrtUserSeq(loginUser.getUserSeq());
        ue.setWrtNm(loginUser.getUserNm());
        ue.setWrtDt(LocalDateTime.now());
        userEduRepository.save(ue);
      }
      userSeq = ue.getUserSeq();
    }
    return userSeq;
  }
  public List<UserEdu> selectEduList(UserEdu userEdu) {
    return userInfoMapper.selectEduList(userEdu);
  }

  @Transactional
  public int deleteEdu(UserEdu userEdu) {
    userEduRepository.deleteById(new UserEdu.UserEduId(userEdu.getEduSeq(), userEdu.getUserSeq()));
    return userEdu.getUserSeq();
  }



  public List<PersonnelStatus> selectPersonnelStatusList(PersonnelStatus personnelStatus) {
    return userInfoMapper.selectPersonnelStatusList(personnelStatus);
  }
  public PersonnelStatus selectPersonnelStatusLastVersion(PersonnelStatus personnelStatus) {
    return personnelStatusRepository
        .findTop1ByYearAndOgCdOrderByVersionNoDesc
            (personnelStatus.getYear(), personnelStatus.getOgCd()).orElse(personnelStatus);
  }

  @Transactional
  public void savePersonnelStatus(UserInfo loginUser, List<PersonnelStatus> personnelStatusList) {
    for(PersonnelStatus status: personnelStatusList){
      PersonnelStatus lastVersion = personnelStatusRepository.findTop1ByYearAndOgCdOrderByVersionNoDesc(status.getYear(), status.getOgCd()).orElse(null);
      status.setVersionNo(lastVersion==null?1:(lastVersion.getVersionNo()+1));
      status.setWrtOrgan(loginUser.getOgCd());
      status.setWrtPart(loginUser.getOfcCd());
      status.setWrtUserSeq(loginUser.getUserSeq());
      status.setWrtUserNm(loginUser.getUserNm());
      status.setWrtUserGrd(loginUser.getTitleCd());
      status.setWrtDt(LocalDateTime.now());
    }
    personnelStatusRepository.saveAll(personnelStatusList);
  }

  public PersonnelStatus nowPersonnelStatus(String ogCd) {
    return userInfoMapper.nowPersonnelStatus(ogCd);
  }

  public List<PersonnelStatus> selectPersonnelStatusHistoryList(PersonnelStatus status) {
    return personnelStatusRepository.findByYearAndOgCdOrderByVersionNoDesc(status.getYear(), status.getOgCd());
  }

  public UserInfo selectPoliceInfo(Integer userSeq) {
    return userInfoMapper.selectPoliceInfo(userSeq);
  }
}
