package com.cloudhandson.vpdbackoffice.web;

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

import com.cloudhandson.vpdbackoffice.domain.effective.EffectiveMatrixView;
import com.cloudhandson.vpdbackoffice.domain.effective.GroupEffectiveAccessView;
import com.cloudhandson.vpdbackoffice.domain.effective.RoleEffectiveImpactView;
import com.cloudhandson.vpdbackoffice.domain.effective.UserEffectiveAccessView;
import java.nio.file.Path;
import java.util.List;
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 EffectiveMatrixTemplateRenderTest {

  @Test
  void rendersTheUserFocusedEffectiveMatrixWithItsLayoutFragments() {
    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 matrix = new EffectiveMatrixView(
        List.of(new UserEffectiveAccessView(
            103L, "김어드민", "E99999", "HQ", true,
            List.of("ALL_DOC_ROLE"), List.of(), List.of(), List.of("ALL_DOC_ROLE"),
            1, List.of("CB_VECTOR_SEARCH_DOCUMENTS"))),
        List.of(new GroupEffectiveAccessView(
            1L, "OPS", "운영", true, List.of("김어드민"), List.of("ALL_DOC_ROLE"),
            1, List.of("CB_VECTOR_SEARCH_DOCUMENTS"))),
        List.of(new RoleEffectiveImpactView(
            30L, "ALL_DOC_ROLE", "CONFIDENTIAL", List.of("김어드민"), List.of(), List.of(),
            List.of("김어드민"), 1, List.of("CB_VECTOR_SEARCH_DOCUMENTS"))),
        1
    );
    var context = new Context(Locale.KOREAN);
    context.setVariable("matrix", matrix);
    context.setVariable("_csrf", new CsrfFixture("_csrf", "test-token"));

    String rendered = engine.process("effective-matrix", context);

    assertThat(rendered)
        .contains("선택한 사용자의 최종 권한")
        .contains("김어드민")
        .contains("data-effective-user-focus")
        .contains("data-inheritance=\"그룹 없음 / 상속 역할 없음\"");
  }

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