package dev.lions.unionflow.server.entity;

import dev.lions.unionflow.server.api.enums.audit.PorteeAudit;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.UUID;

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

@DisplayName("AuditLog")
class AuditLogTest {

    @Test
    @DisplayName("getters/setters de tous les champs")
    void gettersSetters() {
        AuditLog log = new AuditLog();
        log.setTypeAction("CREATE");
        log.setSeverite("INFO");
        log.setUtilisateur("user@test.com");
        log.setRole("ADMIN");
        log.setModule("MEMBRE");
        log.setDescription("Création membre");
        log.setDetails("{\"id\":\"x\"}");
        log.setIpAddress("127.0.0.1");
        log.setUserAgent("Mozilla/5.0");
        log.setSessionId("sess-123");
        LocalDateTime dh = LocalDateTime.now();
        log.setDateHeure(dh);
        log.setDonneesAvant("{}");
        log.setDonneesApres("{\"id\":\"y\"}");
        log.setEntiteId("uuid-1");
        log.setEntiteType("Membre");
        log.setPortee(PorteeAudit.ORGANISATION);

        assertThat(log.getTypeAction()).isEqualTo("CREATE");
        assertThat(log.getSeverite()).isEqualTo("INFO");
        assertThat(log.getUtilisateur()).isEqualTo("user@test.com");
        assertThat(log.getRole()).isEqualTo("ADMIN");
        assertThat(log.getModule()).isEqualTo("MEMBRE");
        assertThat(log.getDescription()).isEqualTo("Création membre");
        assertThat(log.getDetails()).isEqualTo("{\"id\":\"x\"}");
        assertThat(log.getIpAddress()).isEqualTo("127.0.0.1");
        assertThat(log.getUserAgent()).isEqualTo("Mozilla/5.0");
        assertThat(log.getSessionId()).isEqualTo("sess-123");
        assertThat(log.getDateHeure()).isEqualTo(dh);
        assertThat(log.getDonneesAvant()).isEqualTo("{}");
        assertThat(log.getDonneesApres()).isEqualTo("{\"id\":\"y\"}");
        assertThat(log.getEntiteId()).isEqualTo("uuid-1");
        assertThat(log.getEntiteType()).isEqualTo("Membre");
        assertThat(log.getPortee()).isEqualTo(PorteeAudit.ORGANISATION);
    }

    @Test
    @DisplayName("portee par défaut PLATEFORME")
    void porteeDefaut() {
        AuditLog log = new AuditLog();
        assertThat(log.getPortee()).isEqualTo(PorteeAudit.PLATEFORME);
    }

    @Test
    @DisplayName("onCreate initialise dateHeure si null")
    void onCreate_initialiseDateHeure() throws Exception {
        AuditLog log = new AuditLog();
        log.setDateHeure(null);
        Method onCreate = AuditLog.class.getDeclaredMethod("onCreate");
        onCreate.setAccessible(true);
        onCreate.invoke(log);
        assertThat(log.getDateHeure()).isNotNull();
    }

    @Test
    @DisplayName("relation organisation")
    void relationOrganisation() {
        AuditLog log = new AuditLog();
        Organisation o = new Organisation();
        log.setOrganisation(o);
        assertThat(log.getOrganisation()).isSameAs(o);
    }

    @Test
    @DisplayName("héritage BaseEntity: id, actif, etc.")
    void heritageBaseEntity() {
        AuditLog log = new AuditLog();
        UUID id = UUID.randomUUID();
        log.setId(id);
        log.setActif(false);
        assertThat(log.getId()).isEqualTo(id);
        assertThat(log.getActif()).isFalse();
    }
}
