package com.izouma.dingdong.service.backstage;

import com.izouma.dingdong.domain.backstage.Email;
import com.izouma.dingdong.dto.EmailDTO;
import com.izouma.dingdong.repo.UserRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class EmailService {

    private UserRepo userRepo;

    public EmailDTO toDto(Email email) {
        EmailDTO dto = EmailDTO.builder()
                .content(email.getContent())
                .id(email.getId())
                .isRead(email.getIsRead())
                .name(email.getName())
                .sendTime(email.getSendTime())
                .title(email.getTitle())
                .build();

        if (email.getReceiveUserId() != null) {
            userRepo.findById(email.getReceiveUserId()).ifPresent(u -> {
                dto.setReceiveAvatar(u.getAvatar());
                dto.setReceiveNickname(u.getNickname());
            });
        }
        return dto;
    }
}
