package org.bricolages.streaming.preflight;

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

class CreateTableGenerator {
    private StreamDefinitionEntry streamDef;
    CreateTableGenerator(StreamDefinitionEntry streamDef) {
        this.streamDef = streamDef;
    }

    String generate() {
        val sb = new StringBuilder();
        sb.append("--dest-table: ");
        sb.append(streamDef.getFullTableName());
        sb.append("\n\n");
        sb.append("create table $dest_table \n(");
        generateColumnDefinitionList(sb);
        sb.append("\n)\n;\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();
    }
}


@@ -556,16 +556,81 @@
 d(%22%5Cn)%5Cn
+%22);%0A        sb.append(%22sortkey(jst_time)%5Cn%22);%0A        sb.append(%22
 ;%5Cn%22);%0A%0A

Add sortkey to create table stmt