package com.qqchen.deploy.backend.notification.service.impl;

import com.qqchen.deploy.backend.framework.annotation.ServiceType;
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
import com.qqchen.deploy.backend.framework.exception.BusinessException;
import com.qqchen.deploy.backend.framework.exception.UniqueConstraintException;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import com.qqchen.deploy.backend.notification.converter.NotificationChannelConverter;
import com.qqchen.deploy.backend.notification.dto.NotificationChannelDTO;
import com.qqchen.deploy.backend.notification.dto.NotificationChannelQuery;
import com.qqchen.deploy.backend.notification.entity.NotificationChannel;
import com.qqchen.deploy.backend.notification.enums.NotificationChannelStatusEnum;
import com.qqchen.deploy.backend.notification.repository.INotificationChannelRepository;
import com.qqchen.deploy.backend.notification.service.INotificationChannelService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.qqchen.deploy.backend.framework.annotation.ServiceType.Type.DATABASE;

/**
 * 通知渠道Service实现
 *
 * @author qqchen
 * @since 2025-10-22
 */
@Slf4j
@Service
@ServiceType(DATABASE)
public class NotificationChannelServiceImpl 
    extends BaseServiceImpl<NotificationChannel, NotificationChannelDTO, NotificationChannelQuery, Long>
    implements INotificationChannelService {
    
    @Resource
    private INotificationChannelRepository notificationChannelRepository;
    
    @Resource
    private NotificationChannelConverter notificationChannelConverter;
    
    @Override
    protected void validateUniqueConstraints(NotificationChannelDTO dto) {
        // 检查渠道名称唯一性
        if (notificationChannelRepository.existsByNameAndDeletedFalse(dto.getName())) {
            throw new UniqueConstraintException(ResponseCode.CONFLICT, "name", dto.getName());
        }
    }
    
    @Override
    @Transactional
    public boolean testConnection(Long id) {
        NotificationChannel channel = notificationChannelRepository.findById(id)
            .orElseThrow(() -> new BusinessException(ResponseCode.DATA_NOT_FOUND));
        
        // TODO: 实现实际的连接测试逻辑
        log.info("测试通知渠道连接: id={}, type={}", id, channel.getChannelType());
        
        return true;
    }
    
    @Override
    @Transactional
    public void enable(Long id) {
        NotificationChannel channel = notificationChannelRepository.findById(id)
            .orElseThrow(() -> new BusinessException(ResponseCode.DATA_NOT_FOUND));
        
        channel.setStatus(NotificationChannelStatusEnum.ENABLED);
        notificationChannelRepository.save(channel);
        
        log.info("启用通知渠道: id={}, name={}", id, channel.getName());
    }
    
    @Override
    @Transactional
    public void disable(Long id) {
        NotificationChannel channel = notificationChannelRepository.findById(id)
            .orElseThrow(() -> new BusinessException(ResponseCode.DATA_NOT_FOUND));
        
        channel.setStatus(NotificationChannelStatusEnum.DISABLED);
        notificationChannelRepository.save(channel);
        
        log.info("禁用通知渠道: id={}, name={}", id, channel.getName());
    }
}

