package db.migration;


import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.using;

import java.time.LocalDateTime;
import java.util.Map;

import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.jooq.DSLContext;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

public class V0002__initial_import extends BaseJavaMigration {

  @Override
  public void migrate(Context context) throws Exception {

	  try (DSLContext dsl = using(context.getConnection())) {
		  Map<String, Long> roleNameToId = dsl.selectFrom(table("APP_ROLE")).fetchMap(field("NAME", String.class), field("ID", Long.class));

		  PasswordEncoder pe = PasswordEncoderFactories.createDelegatingPasswordEncoder();
		  dsl.insertInto(table("APP_USER"),
				  field("ID"),
				  field("EMAIL"),
				  field("ENABLED"),
				  field("FIRST_NAME"),
				  field("LOCALE"),
				  field("NAME"),
				  field("PASSWORD_HASH"),
				  field("SECRET"),
				  field("USER_NAME"))
		  		  .values(1, "test@test.ch", true, "admin", "en", "admin", pe.encode("admin"), "W4AU5VIXXCPZ3S6T", "admin")
		  		  .execute();

		  dsl.insertInto(table("APP_USER"),
				  field("ID"),
				  field("EMAIL"),
				  field("ENABLED"),
				  field("FIRST_NAME"),
				  field("LOCALE"),
				  field("NAME"),
				  field("PASSWORD_HASH"),
				  field("SECRET"),
				  field("USER_NAME"),
				  field("EXPIRATION_DATE", LocalDateTime.class))
		  		  .values(2, "user@test.ch", true, "user", "de", "user", pe.encode("user"), "LRVLAZ4WVFOU3JBF", "user", LocalDateTime.now().plusYears(1))
		  		  .execute();

		  dsl.insertInto(table("APP_USER"),
				  field("ID"),
				  field("EMAIL"),
				  field("ENABLED"),
				  field("FIRST_NAME"),
				  field("LOCALE"),
				  field("NAME"),
				  field("PASSWORD_HASH"),
				  field("SECRET"),
				  field("USER_NAME"),
				  field("EXPIRATION_DATE", LocalDateTime.class))
		  		  .values(3, "lazy@test.ch", true, "lazy", "en", "lazy", pe.encode("lazy"), null, "lazy", LocalDateTime.now().plusYears(1))
		  		  .execute();


		  dsl.insertInto(table("APP_USER_ROLES"), field("APP_USER_ID"), field("APP_ROLE_ID"))
		          .values(1, roleNameToId.get("ADMIN"))
		          .values(2, roleNameToId.get("USER"))
		          .values(3, roleNameToId.get("USER"))
		          .execute();
      }

  }
}
@@ -1171,16 +1171,76 @@
 R_NAME%22)
+,%0D%0A%09%09          field(%22EXPIRATION_DATE%22, LocalDateTime.class)
 )%0D%0A%09%09  %09
@@ -1350,832 +1350,158 @@
 min%22
-)%0D%0A%09%09  %09%09  .execute();%0D%0A%0D%0A%09%09  dsl.insertInto(table(%22APP_USER%22),%0D%0A%09%09%09%09  field(%22ID%22),%0D%0A%09%09%09%09  field(%22EMAIL%22),%0D%0A%09%09%09%09  field(%22ENABLED%22),%0D%0A%09%09%09%09  field(%22FIRST_NAME%22),%0D%0A%09%09%09%09  field(%22LOCALE%22),%0D%0A%09%09%09%09  field(%22NAME%22),%0D%0A%09%09%09%09  field(%22PASSWORD_HASH%22),%0D%0A%09%09%09%09  field(%22SECRET%22),%0D%0A%09%09%09%09  field(%22USER_NAME%22),%0D%0A%09%09%09%09  field(%22EXPIRATION_DATE%22, LocalDateTime.class))%0D%0A%09%09  %09%09  .values(2, %22user@test.ch%22, true, %22user%22, %22de%22, %22user%22, pe.encode(%22user%22), %22LRVLAZ4WVFOU3JBF%22, %22user%22, LocalDateTime.now().plusYears(1))%0D%0A%09%09  %09%09  .execute();%0D%0A%0D%0A%09%09  dsl.insertInto(table(%22APP_USER%22),%0D%0A%09%09%09%09  field(%22ID%22),%0D%0A%09%09%09%09  field(%22EMAIL%22),%0D%0A%09%09%09%09  field(%22ENABLED%22),%0D%0A%09%09%09%09  field(%22FIRST_NAME%22),%0D%0A%09%09%09%09  field(%22LOCALE%22),%0D%0A%09%09%09%09  field(%22NAME%22),%0D%0A%09%09%09%09  field(%22PASSWORD_HASH%22),%0D%0A%09%09%09%09  field(%22SECRET%22),%0D%0A%09%09%09%09  field(%22USER_NAME%22),%0D%0A%09%09%09%09  field(%22EXPIRATION_DATE%22, LocalDateTime.class
+, null)%0D%0A%09%09  %09%09  .values(2, %22user@test.ch%22, true, %22user%22, %22de%22, %22user%22, pe.encode(%22user%22), %22LRVLAZ4WVFOU3JBF%22, %22user%22, LocalDateTime.now().plusYears(1
 ))%0D%0A
@@ -1654,18 +1654,16 @@
 e();%0D%0A%0D%0A
-%0D%0A
 %09%09  dsl.

Simplify initial data import