package com.aegis.notification.service.channel;

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import com.aegis.notification.domain.Notification;
import com.aegis.notification.mapper.NotificationMapper;

/**
 * 邮件渠道（预留实现）
 * 
 * @author aegis
 */
@Service
public class EmailChannel implements INotificationChannel {
    
    private static final Logger log = LoggerFactory.getLogger(EmailChannel.class);
    
    @Autowired
    private NotificationMapper notificationMapper;
    
    @Autowired(required = false)
    private JavaMailSender mailSender;
    
    @Override
    public boolean send(Notification notification) {
        try {
            // TODO: 使用Spring Mail发送邮件
            // 1. 获取接收者邮箱（从用户信息中获取）
            // 2. 构建邮件内容（HTML格式）
            // 3. 发送邮件
            
            if (mailSender == null) {
                log.warn("邮件服务未配置，跳过邮件发送：通知ID={}", notification.getId());
                notification.setStatus("failed");
                notification.setRemark("邮件服务未配置");
                notificationMapper.updateNotification(notification);
                return false;
            }
            
            // 暂时模拟发送成功
            notification.setStatus("sent");
            notification.setSendTime(new Date());
            notificationMapper.updateNotification(notification);
            
            log.info("邮件通知发送成功：通知ID={}, 接收者={}", notification.getId(), notification.getReceiverId());
            return true;
        } catch (Exception e) {
            log.error("邮件通知发送失败：通知ID={}, 错误={}", notification.getId(), e.getMessage(), e);
            notification.setStatus("failed");
            notification.setRemark("发送失败：" + e.getMessage());
            notificationMapper.updateNotification(notification);
            return false;
        }
    }
    
    @Override
    public String getChannelName() {
        return "email";
    }
    
    @Override
    public boolean supports(String channel) {
        return "email".equals(channel);
    }
}