package dev.lions.unionflow.server.service.centif;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import dev.lions.unionflow.server.service.centif.DosCentifService.DosCentifData;
import dev.lions.unionflow.server.service.centif.DosCentifService.DosOperation;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class GoAmlXmlServiceTest {

  private GoAmlXmlService service;

  @BeforeEach
  void setUp() {
    service = new GoAmlXmlService();
    service.auditTrail = mock(dev.lions.unionflow.server.security.AuditTrailService.class);
  }

  private DosCentifData sampleDos() {
    return new DosCentifData(
        UUID.randomUUID(),
        "DOS-2026-001",
        LocalDateTime.parse("2026-04-25T10:00:00"),
        "Mutuelle Test SFD",
        "SFD",
        "BCEAO-CIV-2025-001",
        "Avenue Test",
        "Abidjan",
        "PERSONNE_PHYSIQUE",
        "Dupont Jean",
        LocalDate.of(1980, 6, 15),
        "CIV",
        "CNI-CIV-12345",
        "Commerçant",
        false,
        null,
        List.of(new DosOperation(LocalDate.of(2026, 4, 20), new BigDecimal("12000000"),
            "XOF", "VIREMENT", "Beneficiaire X")),
        "Pattern de structuration détecté",
        "Compte mis sous surveillance",
        List.of("releve_compte.pdf"));
  }

  @Test
  @DisplayName("Génère un XML valide non vide")
  void genererXmlNonVide() throws IOException {
    byte[] xml = service.genererXml(sampleDos());
    assertThat(xml).isNotEmpty();
    String content = new String(xml);
    assertThat(content).startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    assertThat(content).contains("<report ");
    assertThat(content).contains("</report>");
  }

  @Test
  @DisplayName("XML contient l'ID DOS, déclarant et transaction")
  void xmlContientChampsClefs() throws IOException {
    String content = new String(service.genererXml(sampleDos()));
    assertThat(content).contains("DOS-2026-001");
    assertThat(content).contains("Mutuelle Test SFD");
    assertThat(content).contains("STR"); // report code
    assertThat(content).contains("XOF");
    assertThat(content).contains("12000000");
    assertThat(content).contains("Pattern de structuration");
  }

  @Test
  @DisplayName("Reporting person anonymisé envers procureur")
  void reportingPersonAnonyme() throws IOException {
    String content = new String(service.genererXml(sampleDos()));
    assertThat(content).contains("[REDACTED]");
    assertThat(content).contains("Compliance Officer");
  }

  @Test
  @DisplayName("Pays de l'institution = CIV")
  void paysIvoirien() throws IOException {
    String content = new String(service.genererXml(sampleDos()));
    assertThat(content).contains("<country_code>CIV</country_code>");
  }
}
