/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.gravitino.spark.connector.version;

import com.google.common.collect.ImmutableMap;
import java.util.Locale;
import java.util.Map;
import org.apache.spark.package$;
import org.apache.spark.util.VersionUtils$;

public class CatalogNameAdaptor {
  private static final Map<String, String> catalogNames;

  static {
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    catalogNames =
        builder
            .put(
                "hive-3.3", "org.apache.gravitino.spark.connector.hive.GravitinoHiveCatalogSpark33")
            .put(
                "hive-3.4", "org.apache.gravitino.spark.connector.hive.GravitinoHiveCatalogSpark34")
            .put(
                "hive-3.5", "org.apache.gravitino.spark.connector.hive.GravitinoHiveCatalogSpark35")
            .put(
                "lakehouse-iceberg-3.3",
                "org.apache.gravitino.spark.connector.iceberg.GravitinoIcebergCatalogSpark33")
            .put(
                "lakehouse-iceberg-3.4",
                "org.apache.gravitino.spark.connector.iceberg.GravitinoIcebergCatalogSpark34")
            .put(
                "lakehouse-iceberg-3.5",
                "org.apache.gravitino.spark.connector.iceberg.GravitinoIcebergCatalogSpark35")
            .put(
                "lakehouse-paimon-3.3",
                "org.apache.gravitino.spark.connector.paimon.GravitinoPaimonCatalogSpark33")
            .put(
                "lakehouse-paimon-3.4",
                "org.apache.gravitino.spark.connector.paimon.GravitinoPaimonCatalogSpark34")
            .put(
                "lakehouse-paimon-3.5",
                "org.apache.gravitino.spark.connector.paimon.GravitinoPaimonCatalogSpark35")
            .put(
                "glue-3.3", "org.apache.gravitino.spark.connector.glue.GravitinoGlueCatalogSpark33")
            .put(
                "glue-3.4", "org.apache.gravitino.spark.connector.glue.GravitinoGlueCatalogSpark34")
            .put(
                "glue-3.5", "org.apache.gravitino.spark.connector.glue.GravitinoGlueCatalogSpark35")
            .build();
  }

  private static final Map<String, String> jdbcCatalogNames =
      ImmutableMap.of(
          "3.3",
          "org.apache.gravitino.spark.connector.jdbc.GravitinoJdbcCatalogSpark33",
          "3.4",
          "org.apache.gravitino.spark.connector.jdbc.GravitinoJdbcCatalogSpark34",
          "3.5",
          "org.apache.gravitino.spark.connector.jdbc.GravitinoJdbcCatalogSpark35");

  private static final Map<String, String> pgCatalogNames =
      ImmutableMap.of(
          "3.3",
          "org.apache.gravitino.spark.connector.jdbc.postgresql.GravitinoPostgreSqlCatalogSpark33",
          "3.4",
          "org.apache.gravitino.spark.connector.jdbc.postgresql.GravitinoPostgreSqlCatalogSpark34",
          "3.5",
          "org.apache.gravitino.spark.connector.jdbc.postgresql.GravitinoPostgreSqlCatalogSpark35");

  private static String sparkVersion() {
    return package$.MODULE$.SPARK_VERSION();
  }

  private static String getCatalogName(String provider, int majorVersion, int minorVersion) {
    if (provider.startsWith("jdbc")) {
      if (provider.startsWith("jdbc-postgresql")) {
        return pgCatalogNames.get(String.format("%d.%d", majorVersion, minorVersion));
      }
      return jdbcCatalogNames.get(String.format("%d.%d", majorVersion, minorVersion));
    }
    String key =
        String.format("%s-%d.%d", provider.toLowerCase(Locale.ROOT), majorVersion, minorVersion);
    return catalogNames.get(key);
  }

  public static String getCatalogName(String provider) {
    int majorVersion = VersionUtils$.MODULE$.majorVersion(sparkVersion());
    int minorVersion = VersionUtils$.MODULE$.minorVersion(sparkVersion());
    return getCatalogName(provider, majorVersion, minorVersion);
  }
}
