package com.njcn.gather.systemops.database.support.spi;

import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 数据库能力路由注册器。
 */
@Component
@RequiredArgsConstructor
public class DatabaseOperatorRegistry {

    private final List<DatabaseConnectionOperator> connectionOperators;
    private final List<DatabaseBackupOperator> backupOperators;
    private final List<DatabaseRestoreOperator> restoreOperators;

    public DatabaseConnectionOperator getConnectionOperator(String dbType) {
        return connectionOperators.stream()
                .filter(operator -> operator.support(dbType))
                .findFirst()
                .orElseThrow(() -> new BusinessException(CommonResponseEnum.FAIL, "暂不支持的数据库类型：" + dbType));
    }

    public DatabaseBackupOperator getBackupOperator(String dbType, String backupStrategy) {
        return backupOperators.stream()
                .filter(operator -> operator.support(dbType, backupStrategy))
                .findFirst()
                .orElseThrow(() -> new BusinessException(CommonResponseEnum.FAIL,
                        "暂不支持的备份能力：" + dbType + "/" + backupStrategy));
    }

    public DatabaseRestoreOperator getRestoreOperator(String dbType, String backupStrategy) {
        return restoreOperators.stream()
                .filter(operator -> operator.support(dbType, backupStrategy))
                .findFirst()
                .orElseThrow(() -> new BusinessException(CommonResponseEnum.FAIL,
                        "暂不支持的恢复能力：" + dbType + "/" + backupStrategy));
    }
}
