package com.cloudhandson.vpdbackoffice.web;

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

import com.cloudhandson.vpdbackoffice.domain.probe.FgaExecutionEvidence;
import com.cloudhandson.vpdbackoffice.domain.probe.ProbeResult;
import com.cloudhandson.vpdbackoffice.domain.probe.ProbeStatus;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.templateresolver.FileTemplateResolver;

class ProbeResultTemplateRenderTest {

  @Test
  void rendersFgaEvidenceAndUnavailableMessageAsTextNotAsABoolean() {
    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);

    ProbeResult result = new ProbeResult(
        ProbeStatus.SUCCESS,
        List.of("DOC_ID"),
        List.of(Map.of("DOC_ID", 1)),
        1,
        List.of(),
        null,
        null,
        "{}",
        "{}",
        "{}",
        "{}"
    ).withFgaExecutionEvidence(new FgaExecutionEvidence(
        "2026-07-01T15:00:00Z",
        "CB_ORDS",
        "8db9fc64-c71b-4b9e-9514-2fcaa355d9f3",
        "SELECT",
        "SELECT * FROM ADMIN.CB_V_SEARCH_DOCUMENTS",
        "CB_AGENT_DOC_POLICY: (DEPT_CODE = 'HR')"
    ), null);
    var context = new Context(Locale.KOREAN);
    context.setVariable("result", result);
    context.setVariable("vectorSearch", false);
    context.setVariable("tokenContext", null);
    context.setVariable("selectedObject", null);

    String rendered = engine.process("fragments/probe-result", context);

    assertThat(rendered)
        .contains("DB 감사 실행 증적")
        .contains("8db9fc64-c71b-4b9e-9514-2fcaa355d9f3")
        .contains("SELECT * FROM ADMIN.CB_V_SEARCH_DOCUMENTS")
        .contains("CB_AGENT_DOC_POLICY: (DEPT_CODE = &#39;HR&#39;)");
  }
}
