package com.qqchen.deploy.backend.notification.factory;

import com.qqchen.deploy.backend.notification.adapter.INotificationChannelAdapter;
import com.qqchen.deploy.backend.notification.enums.NotificationChannelTypeEnum;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知渠道适配器工厂
 *
 * @author qqchen
 * @since 2025-11-03
 */
@Slf4j
@Component
public class NotificationChannelAdapterFactory {
    
    @Resource
    private List<INotificationChannelAdapter> adapters;
    
    private final Map<NotificationChannelTypeEnum, INotificationChannelAdapter> adapterMap = new HashMap<>();
    
    /**
     * 初始化适配器映射
     */
    @PostConstruct
    public void init() {
        for (INotificationChannelAdapter adapter : adapters) {
            NotificationChannelTypeEnum type = adapter.supportedType();
            adapterMap.put(type, adapter);
            log.info("注册通知渠道适配器: {} -> {}", type, adapter.getClass().getSimpleName());
        }
    }
    
    /**
     * 根据渠道类型获取适配器
     *
     * @param type 渠道类型
     * @return 适配器实例
     * @throws IllegalArgumentException 如果不支持该渠道类型
     */
    public INotificationChannelAdapter getAdapter(NotificationChannelTypeEnum type) {
        INotificationChannelAdapter adapter = adapterMap.get(type);
        
        if (adapter == null) {
            throw new IllegalArgumentException("不支持的通知渠道类型: " + type);
        }
        
        return adapter;
    }
}

