package com.kospo.talk.service.impl;

import com.kospo.talk.config.exception.ErrorCode;
import com.kospo.talk.exception.CustomException;
import com.kospo.talk.model.PersonAutoReply;
import com.kospo.talk.model.SmUser;
import com.kospo.talk.model.SmUserId;
import com.kospo.talk.model.User;
import com.kospo.talk.payload.PersonAutoReplyDto;
import com.kospo.talk.repository.PersonAutoReplyRepository;
import com.kospo.talk.repository.SmUserRepository;
import com.kospo.talk.service.PersonAutoReplyService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class PersonAutoReplyServiceImpl implements PersonAutoReplyService {
    private final PersonAutoReplyRepository personAutoReplyRepository;
    private final SmUserRepository smUserRepository;

    @Override
    public List<PersonAutoReplyDto> findByWorkAndSabunAndLikeContent(PersonAutoReplyDto personAutoReplyDto) {
        List<PersonAutoReply> personAutoReplies = null;
        if(!personAutoReplyDto.getContent().isBlank() && !personAutoReplyDto.getContent().isEmpty()) {
            personAutoReplies = personAutoReplyRepository.findBySabunAndContentLikeOrderByInsDateDesc(personAutoReplyDto.getSabun(), personAutoReplyDto.getContent());
        }  else {
            personAutoReplies = personAutoReplyRepository.findBySabunOrderByInsDateDesc(personAutoReplyDto.getSabun());
        }
        return personAutoReplies.stream().map(PersonAutoReplyDto::to).toList();
    }

    @Override
    @Transactional
    public void save(PersonAutoReplyDto personAutoReplyDto) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        personAutoReplyDto.setSabun(user.getSabun());
        SmUser smUser = smUserRepository.findById(SmUserId.builder()
                .workId(personAutoReplyDto.getWorkId())
                .sabun(personAutoReplyDto.getSabun()).build()
        ).get();

        if(!smUser.getName().isEmpty() && !smUser.getName().isBlank()) {
            personAutoReplyDto.setInsDate(LocalDateTime.now());
            personAutoReplyRepository.save(PersonAutoReply.to(personAutoReplyDto));
        }
    }

    @Override
    @Transactional
    public void update(PersonAutoReplyDto personAutoReplyDto) {
        PersonAutoReply checker = personAutoReplyRepository.findById(personAutoReplyDto.getId()).get();
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(checker.getSabun().equals(user.getSabun())) {
            checker.setContent(personAutoReplyDto.getContent());
            personAutoReplyRepository.save(checker);
        } else {
            throw new CustomException(ErrorCode.AUTHENTICATION);
        }
    }

    @Override
    @Transactional
    public void delete(int id) {
        PersonAutoReply checker = personAutoReplyRepository.findById(id).get();
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(checker.getSabun().equals(user.getSabun())) {
            personAutoReplyRepository.delete(checker);;
        } else {
            throw new CustomException(ErrorCode.AUTHENTICATION);
        }
    }
}
