package com.erp.file.service.storage;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

/**
 * 存储策略工厂
 */
@Slf4j
@Component
public class StorageFactory {
    
    @Value("${file.storage.default:local}")
    private String defaultStorage;
    
    private final Map<String, StorageStrategy> strategies = new HashMap<>();
    
    public StorageFactory(List<StorageStrategy> storageStrategies) {
        for (StorageStrategy strategy : storageStrategies) {
            strategies.put(strategy.getType(), strategy);
            log.info("注册存储策略: {}", strategy.getType());
        }
    }
    
    /**
     * 获取默认存储策略
     */
    public StorageStrategy getDefaultStrategy() {
        return getStrategy(defaultStorage);
    }
    
    /**
     * 获取指定类型的存储策略
     */
    public StorageStrategy getStrategy(String type) {
        StorageStrategy strategy = strategies.get(type);
        if (strategy == null) {
            log.warn("未找到存储策略: {}，使用默认策略: {}", type, defaultStorage);
            return getDefaultStrategy();
        }
        return strategy;
    }
    
    /**
     * 获取所有可用的存储策略
     */
    public Map<String, StorageStrategy> getAllStrategies() {
        return new HashMap<>(strategies);
    }
    
    /**
     * 检查存储策略是否可用
     */
    public boolean isStrategyAvailable(String type) {
        return strategies.containsKey(type);
    }
}
