package org.bricolages.streaming.preflight;

import java.util.StringJoiner;
import lombok.*;

class CreateTableGenerator {
    private final StreamDefinitionEntry streamDef;
    private final String fullTableName;
    CreateTableGenerator(StreamDefinitionEntry streamDef, String fullTableName) {
        this.streamDef = streamDef;
        this.fullTableName = fullTableName;
    }

    String generate() {
        val sb = new StringBuilder();
        sb.append("--dest-table: ");
        sb.append(fullTableName);
        sb.append("\n\n");
        sb.append("create table $dest_table \n(");
        generateColumnDefinitionList(sb);
        sb.append("\n)\n");
        sb.append("sortkey(jst_time)\n");
        sb.append(";\n");

        return sb.toString();
    }

    void generateColumnDefinitionList(StringBuilder sb) {
        val sj = new StringJoiner("\n,");
        for (val columnDef: streamDef.getColumns()) {
            sj.add(generateColumnDefinition(columnDef));
        }
        sb.append(sj.toString());
    }

    String generateColumnDefinition(ColumnDefinitionEntry columnDef) {
        val sj = new StringJoiner(" ", " ", "");
        sj.add(columnDef.getColumName());
        sj.add(columnDef.getParams().getType());
        sj.add("encode");
        sj.add(columnDef.getParams().getEncoding().toString());
        
        return sj.toString();
    }
}


@@ -37,17 +37,16 @@
 flight;%0A
-%0A
 import j
@@ -86,16 +86,41 @@
 bok.*;%0A%0A
+@RequiredArgsConstructor%0A
 class Cr
@@ -140,32 +140,24 @@
 rator %7B%0A    
-private 
 final Stream
@@ -191,16 +191,8 @@
     
-private 
 fina
@@ -218,176 +218,8 @@
 ame;
-%0A    CreateTableGenerator(StreamDefinitionEntry streamDef, String fullTableName) %7B%0A        this.streamDef = streamDef;%0A        this.fullTableName = fullTableName;%0A    %7D
 %0A%0A  
@@ -419,17 +419,16 @@
 st_table
- 
 %5Cn(%22);%0A 
@@ -492,24 +492,63 @@
 d(%22%5Cn)%5Cn%22);%0A
+        sb.append(%22diststyle even%5Cn%22);%0A
         sb.a
@@ -561,16 +561,17 @@
 %22sortkey
+ 
 (jst_tim
@@ -1252,9 +1252,8 @@
     %7D%0A%7D%0A
-%0A

Generates diststyle for .ct