package io.quarkus.cli;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import io.quarkus.cli.registry.BaseRegistryCommand;
import io.quarkus.registry.config.RegistriesConfig;
import io.quarkus.registry.config.RegistryConfig;
import picocli.CommandLine;

@CommandLine.Command(name = "add", header = "Add a Quarkus extension registry", description = "%n"
        + "This command will add a Quarkus extension registry to the registry client configuration unless it's already present.")
public class RegistryAddCommand extends BaseRegistryCommand {

    @CommandLine.Parameters(arity = "1..*", split = ",", paramLabel = "REGISTRY-ID[,REGISTRY-ID]", description = "Registry ID to add to the registry client configuration%n"
            + "  Example:%n"
            + "    registry.quarkus.io%n"
            + "    registry.quarkus.acme.com,registry.quarkus.io%n")
    List<String> registryIds;

    @Override
    public Integer call() throws Exception {
        boolean existingConfig = false;
        Path configYaml = null;

        // If a configuration was specified, check if it exists
        if (registryClient.getConfigArg() != null) {
            configYaml = Paths.get(registryClient.getConfigArg());
            existingConfig = Files.exists(configYaml);
        }

        final RegistriesConfig.Mutable config;
        if (configYaml != null && !existingConfig) {
            // we're creating a new configuration for a new file
            config = RegistriesConfig.builder();
        } else {
            config = registryClient.resolveConfig().mutable();
        }
        registryClient.refreshRegistryCache(output);

        boolean persist = false;
        for (String registryId : registryIds) {
            persist |= config.addRegistry(registryId);
        }

        if (persist) {
            output.printText("Configured registries:");
            for (RegistryConfig rc : config.getRegistries()) {
                output.printText("- " + rc.getId());
            }
            if (configYaml != null) {
                config.persist(configYaml);
            } else {
                config.persist();
            }
        }
        return CommandLine.ExitCode.OK;
    }
}

@@ -18,16 +18,82 @@
 s.cli;%0A%0A
+import static io.quarkus.registry.Constants.DEFAULT_REGISTRY_ID;%0A%0A
 import j
@@ -2035,24 +2035,474 @@
 stries()) %7B%0A
+                if (!existingConfig && config.getRegistries().size() == 1%0A                        && !rc.getId().equals(DEFAULT_REGISTRY_ID)) %7B%0A                    output.warn(%0A                            rc.getId() + %22 is the only registry configured in the config file.%5Cn%22 + rc.getId()%0A                                    + %22 replaced the Default registry: %22%0A                                    + DEFAULT_REGISTRY_ID);%0A                %7D else %7B%0A    
             
@@ -2538,24 +2538,43 @@
 c.getId());%0A
+                %7D%0A%0A
             

add warning if no registry set.