/*
 * 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 io.trino.tests.product.launcher.env;

import io.trino.tests.product.launcher.docker.DockerFiles.ResourceProvider;

import static org.testcontainers.utility.MountableFile.forHostPath;

public final class EnvironmentContainers
{
    public static final String PRESTO = "presto";
    public static final String COORDINATOR = PRESTO + "-master";
    public static final String WORKER = PRESTO + "-worker";
    public static final String WORKER_NTH = WORKER + "-";
    public static final String HADOOP = "hadoop-master";
    public static final String TESTS = "tests";
    public static final String LDAP = "ldapserver";

    private EnvironmentContainers() {}

    public static String worker(int number)
    {
        return WORKER_NTH + number;
    }

    public static boolean isPrestoContainer(String name)
    {
        return name.startsWith(PRESTO);
    }

    /**
     * Use this method only when you place `tempto-configuration.yaml' in environment configuration directory.
     */
    public static void configureTempto(Environment.Builder builder, ResourceProvider configDir)
    {
        builder.configureContainer(TESTS, dockerContainer -> {
            String environmentName = configDir.getPath("..").toFile().getName();
            String temptoConfig = "/docker/presto-product-tests/conf/tempto/tempto-configuration-for-" + environmentName + ".yaml";
            dockerContainer
                    .withCopyFileToContainer(
                            forHostPath(configDir.getPath("tempto-configuration.yaml")),
                            temptoConfig)
                    .withEnv("TEMPTO_CONFIG_FILES", temptoConfigFiles ->
                            temptoConfigFiles
                                    .map(files -> files + "," + temptoConfig)
                                    .orElse(temptoConfig));
        });
    }
}

@@ -689,112 +689,477 @@
 ort 
-static org.testcontainers.utility.MountableFile.forHostPath;%0A%0Apublic final class EnvironmentContainers%0A%7B
+java.nio.file.Path;%0Aimport java.security.SecureRandom;%0A%0Aimport static com.google.common.base.Preconditions.checkArgument;%0Aimport static java.lang.Character.MAX_RADIX;%0Aimport static java.lang.Math.abs;%0Aimport static java.lang.Math.min;%0Aimport static org.testcontainers.utility.MountableFile.forHostPath;%0A%0Apublic final class EnvironmentContainers%0A%7B%0A    private static final SecureRandom random = new SecureRandom();%0A    private static final int RANDOM_SUFFIX_LENGTH = 5;%0A
 %0A   
@@ -2097,73 +2097,145 @@
     
-String environmentName = configDir.getPath(%22..%22).toFile().getName
+Path path = configDir.getPath(%22tempto-configuration.yaml%22);%0A            String suffix = getParentDirectoryName(path) + %22-%22 + randomSuffix
 ();%0A
@@ -2343,23 +2343,14 @@
 %22 + 
-environmentName
+suffix
  + %22
@@ -2434,125 +2434,26 @@
 ner(
-%0A                            forHostPath(configDir.getPath(%22tempto-configuration.yaml%22)),%0A                           
+forHostPath(path),
  tem
@@ -2737,11 +2737,474 @@
 );%0A    %7D
+%0A%0A    private static String getParentDirectoryName(Path path)%0A    %7B%0A        checkArgument(path.getNameCount() %3E= 2, %22Cannot determine parent directory of: %25s%22, path);%0A        return path.getName(path.getNameCount() - 2).toString();%0A    %7D%0A%0A    private static String randomSuffix()%0A    %7B%0A        String randomSuffix = Long.toString(abs(random.nextLong()), MAX_RADIX);%0A        return randomSuffix.substring(0, min(RANDOM_SUFFIX_LENGTH, randomSuffix.length()));%0A    %7D
 %0A%7D%0A

Fix file path generation in configureTempto() method