package com.cloudhandson.vpdbackoffice.web;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Path;
import java.util.Locale;
import org.junit.jupiter.api.Test;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.templateresolver.FileTemplateResolver;

class SettingsTemplateRenderTest {

  @Test
  void rendersConnectionAndDatabasePreparationPagesSeparately() {
    var resolver = new FileTemplateResolver();
    resolver.setPrefix(Path.of("src/main/resources/templates").toAbsolutePath() + "/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode("HTML");
    resolver.setCacheable(false);
    var engine = new SpringTemplateEngine();
    engine.setTemplateResolver(resolver);

    var context = new Context(Locale.KOREAN);
    context.setVariable("_csrf", new CsrfFixture("_csrf", "test-token"));
    context.setVariable("ordsBaseUrl", "https://ords.example.test/ords");

    String connection = engine.process("settings", context);
    String database = engine.process("settings-database", context);

    assertThat(connection)
        .contains("ORDS Base URL")
        .contains("/settings/database")
        .doesNotContain("/settings/database/initialize");
    assertThat(database)
        .contains("DB 준비 상태")
        .contains("/settings/database/initialize")
        .contains("DB 준비 실행");
  }

  private record CsrfFixture(String parameterName, String token) {
  }
}
