package com.kospo.talk.service.impl;

import com.kospo.talk.model.Talk;
import com.kospo.talk.model.TalkMember;
import com.kospo.talk.model.TalkMemberId;
import com.kospo.talk.payload.TalkMemberDto;
import com.kospo.talk.payload.ITalkMemberDto;
import com.kospo.talk.repository.TalkMemberRepository;
import com.kospo.talk.repository.TalkRepository;
import com.kospo.talk.service.TalkMemberService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TalkMemberServiceImpl implements TalkMemberService {
    private final TalkMemberRepository talkMemberRepository;
    private final TalkRepository talkRepository;

    @Transactional
    @Override
    public List<ITalkMemberDto> findBySabun(String sabun) {
        return talkMemberRepository.findBySabun(sabun);
    }

    @Override
    public List<String> findBySabunGroupBySabun(String sabun) {
        return talkMemberRepository.findBySabunGroupBySabun(sabun);
    }


    @Transactional
    @Override
    public List<ITalkMemberDto> findByTalkId(String talkId) {
        return talkMemberRepository.findByTalkId(talkId);
    }

    @Override
    public List<TalkMember> findAllByTalkId(String talkId) {
        return talkMemberRepository.findByIdTalkId(talkId);
    }

    @Override
    public void saveAll(List<TalkMember> talkMembers) {
        talkMemberRepository.saveAll(talkMembers);
    }

    @Override
    public void save(TalkMemberDto talkMemberDto) {
        TalkMember talkMember = TalkMember.to(talkMemberDto);
        talkMemberRepository.save(talkMember);
    }

    @Override
    public boolean updateMark(String talkId, String sabun) {
        TalkMember cm = talkMemberRepository.findById(TalkMemberId.builder()
                .talkId(talkId)
                .sabun(sabun).build()).get();

        cm.setMarkYn(!cm.getMarkYn());
        talkMemberRepository.save(cm);
        return !cm.getMarkYn();
    }

    @Override
    public boolean updateDelete(String talkId, String sabun) {
        Talk talk = talkRepository.findById(talkId).get();
        if(talk.getCloseYn()) {
            TalkMember cm = talkMemberRepository.findById(TalkMemberId.builder()
                    .talkId(talkId)
                    .sabun(sabun).build()).get();

            cm.setDeleteYn(true);
            talkMemberRepository.save(cm);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<ITalkMemberDto> findTalkOnlineYnInSabun(String sabun) {
        return talkMemberRepository.findTalkOnlineYnInSabun(sabun);
    }

    @Override
    public List<TalkMember> findDeleteTarget(String talkId) {
        return talkMemberRepository.findDeleteTarget(talkId);
    }

    @Override
    public void deleteMember(String talkId, String sabun) {
        talkMemberRepository.deleteById(TalkMemberId.builder().talkId(talkId).sabun(sabun).build());
    }

    @Override
    public void disableTalkId(String talkId) {
        talkMemberRepository.disableTalkId(talkId);
    }

    @Override
    public void enableTalkIdAndSabun(String talkId, String sabun) {
        talkMemberRepository.enableTalkIdAndSabun(talkId, sabun);
    }
}
