package com.devjob.notification.service;

import com.devjob.notification.dto.EmailRequest;
import com.devjob.notification.dto.NotificationResponse;
import com.devjob.notification.repository.NotificationRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class NotificationService {

    private final EmailService emailService;
    private final NotificationRepository notificationRepository;

    @Transactional
    public NotificationResponse sendEmail(EmailRequest req) {
        var notification = emailService.send(req.userId(), req.recipientEmail(), req.subject(), req.content());
        return NotificationResponse.from(notification);
    }

    public List<NotificationResponse> findMy(Long userId) {
        return notificationRepository.findByUserIdOrderByCreatedAtDesc(userId).stream()
                .map(NotificationResponse::from)
                .toList();
    }
}
