package bio.terra.service.dataset;

import bio.terra.common.PdaoConstant;
import bio.terra.model.DatasetRequestModel;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.stringtemplate.v4.ST;

public final class DatasetUtils {

  // Only allow usage of static methods.
  private DatasetUtils() {}

  private static final String auxTableNamePattern =
      PdaoConstant.PDAO_PREFIX + "<auxId>_<table>_<randomSuffix>";

  /**
   * Generate a semi-random name for an 'auxiliary' BigQuery table to support a user-defined table.
   *
   * <p>'Auxiliary' tables can store helper information during ingest / at runtime. For example:
   * staging tables, "raw" data tables, soft-delete lists.
   *
   * @param table user-defined table to generate an aux name for
   * @param infixId identifier to include in the aux table name, to help distinguish it in the
   *     BigQuery UI
   */
  public static String generateAuxTableName(DatasetTable table, String infixId) {
    ST nameTemplate = new ST(auxTableNamePattern);
    nameTemplate.add("table", table.getName());
    nameTemplate.add("auxId", infixId);
    nameTemplate.add(
        "randomSuffix", StringUtils.replaceChars(UUID.randomUUID().toString(), '-', '_'));
    return nameTemplate.render();
  }

  /**
   * Convert a dataset request into a fully-populated dataset model.
   *
   * <p>Names for "raw" and "soft-delete" tables will be generated and injected into the model after
   * it is parsed from the base request. Since those generated names are semi-random, calling this
   * method twice on the same request will produce different results.
   */
  public static Dataset convertRequestWithGeneratedNames(DatasetRequestModel request) {
    Dataset baseDataset = DatasetJsonConversion.datasetRequestToDataset(request);
    baseDataset
        .getTables()
        .forEach(
            t -> {
              t.rawTableName(generateAuxTableName(t, "raw"));
              t.softDeleteTableName(generateAuxTableName(t, "sd"));
              t.rowMetadataTableName(generateAuxTableName(t, "row_metadata"));
            });
    return baseDataset;
  }
}

@@ -438,22 +438,132 @@
 fix%3E%22;%0A%0A
+  private static final String metadataTableNamePattern =%0A      PdaoConstant.PDAO_PREFIX + %22%3CauxId%3E_%3Ctable%3E%22;%0A%0A
   /**%0A
-
    * Gen
@@ -1380,16 +1380,286 @@
 );%0A  %7D%0A%0A
+  public static String generateMetadataTableName(DatasetTable table, String infixId) %7B%0A    ST nameTemplate = new ST(metadataTableNamePattern);%0A    nameTemplate.add(%22table%22, table.getName());%0A    nameTemplate.add(%22auxId%22, infixId);%0A    return nameTemplate.render();%0A  %7D%0A%0A
   /**%0A  
@@ -2417,35 +2417,40 @@
 bleName(generate
-Aux
+Metadata
 TableName(t, %22ro

remove suffix for row metadata table (#1163)