package dev.lions.unionflow.server.repository;

import dev.lions.unionflow.server.api.enums.audit.PorteeAudit;
import dev.lions.unionflow.server.entity.AuditLog;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.TestTransaction;
import jakarta.inject.Inject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.UUID;

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

@QuarkusTest
class AuditLogRepositoryTest {

    @Inject
    AuditLogRepository auditLogRepository;

    private static AuditLog newAuditLog() {
        AuditLog a = new AuditLog();
        a.setTypeAction("CREATE");
        a.setSeverite("INFO");
        a.setUtilisateur("test@test.com");
        a.setModule("TEST");
        a.setDescription("Test audit");
        a.setPortee(PorteeAudit.PLATEFORME);
        return a;
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve le log")
    void persist_thenFindById_findsLog() {
        AuditLog log = newAuditLog();
        auditLogRepository.persist(log);
        assertThat(log.getId()).isNotNull();
        AuditLog found = auditLogRepository.findById(log.getId());
        assertThat(found).isNotNull();
        assertThat(found.getTypeAction()).isEqualTo("CREATE");
    }

    @Test
    @TestTransaction
    @DisplayName("findById retourne null pour UUID inexistant")
    void findById_inexistant_returnsNull() {
        assertThat(auditLogRepository.findById(UUID.randomUUID())).isNull();
    }

    @Test
    @TestTransaction
    @DisplayName("listAll retourne une liste")
    void listAll_returnsList() {
        List<AuditLog> list = auditLogRepository.listAll();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("count retourne un nombre >= 0")
    void count_returnsNonNegative() {
        assertThat(auditLogRepository.count()).isGreaterThanOrEqualTo(0L);
    }

    @Test
    @TestTransaction
    @DisplayName("deleteById supprime le log")
    void deleteById_removesLog() {
        AuditLog log = newAuditLog();
        auditLogRepository.persist(log);
        UUID id = log.getId();
        boolean deleted = auditLogRepository.deleteById(id);
        assertThat(deleted).isTrue();
        assertThat(auditLogRepository.findById(id)).isNull();
    }
}
