/*
 * Copyright 2025 Apollo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.ctrip.framework.apollo.configservice.service.config;

import com.ctrip.framework.apollo.biz.entity.Release;
import com.ctrip.framework.apollo.biz.entity.ReleaseMessage;
import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolder;
import com.ctrip.framework.apollo.biz.service.ReleaseService;
import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * config service with no cache
 *
 * @author Jason Song(song_s@ctrip.com)
 */
public class DefaultConfigService extends AbstractConfigService {

  private final ReleaseService releaseService;
  private final GrayReleaseRulesHolder grayReleaseRulesHolder;

  public DefaultConfigService(final ReleaseService releaseService,
      final GrayReleaseRulesHolder grayReleaseRulesHolder) {
    super(grayReleaseRulesHolder);
    this.releaseService = releaseService;
    this.grayReleaseRulesHolder = grayReleaseRulesHolder;
  }

  @Override
  protected Release findActiveOne(long id, ApolloNotificationMessages clientMessages) {
    return releaseService.findActiveOne(id);
  }

  @Override
  protected Release findLatestActiveRelease(String configAppId, String configClusterName,
      String configNamespace, ApolloNotificationMessages clientMessages) {
    return releaseService.findLatestActiveRelease(configAppId, configClusterName, configNamespace);
  }

  @Override
  public void handleMessage(ReleaseMessage message, String channel) {
    // since there is no cache, so do nothing
  }

  @Override
  public Map<String, Release> findReleasesByReleaseKeys(Set<String> releaseKeys) {
    List<Release> releasesMap = releaseService.findByReleaseKeys(releaseKeys);
    if (releasesMap != null) {
      return releasesMap.stream()
          .collect(ImmutableMap.toImmutableMap(Release::getReleaseKey, release -> release));
    }
    return Collections.emptyMap();
  }
}
