package dev.lions.user.manager.service.impl;

import dev.lions.user.manager.dto.audit.AuditLogDTO;
import dev.lions.user.manager.enums.audit.TypeActionAudit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Tests supplémentaires pour AuditServiceImpl pour améliorer la couverture
 */
class AuditServiceImplAdditionalTest {

    private AuditServiceImpl auditService;

    @BeforeEach
    void setUp() {
        auditService = new AuditServiceImpl();
        auditService.auditEnabled = true;
        auditService.logToDatabase = false;
    }

    @Test
    void testFindByActeur_WithDates() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");
        auditService.logSuccess(TypeActionAudit.USER_UPDATE, "USER", "2", "user2", "realm1", "admin", "Updated");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime past = now.minusDays(1);
        LocalDateTime future = now.plusDays(1);

        List<AuditLogDTO> logs = auditService.findByActeur("admin", past, future, 0, 10);

        assertNotNull(logs);
        assertTrue(logs.size() > 0);
    }

    @Test
    void testFindByRealm_WithDates() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime past = now.minusDays(1);
        LocalDateTime future = now.plusDays(1);

        List<AuditLogDTO> logs = auditService.findByRealm("realm1", past, future, 0, 10);

        assertNotNull(logs);
    }

    @Test
    void testFindByRessource() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime past = now.minusDays(1);
        LocalDateTime future = now.plusDays(1);

        List<AuditLogDTO> logs = auditService.findByRessource("USER", "1", past, future, 0, 10);

        assertNotNull(logs);
    }

    @Test
    void testCountByActionType() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "2", "user2", "realm1", "admin", "Created");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime past = now.minusDays(1);
        LocalDateTime future = now.plusDays(1);

        java.util.Map<TypeActionAudit, Long> counts = auditService.countByActionType("realm1", past, future);

        assertNotNull(counts);
        assertTrue(counts.containsKey(TypeActionAudit.USER_CREATE));
    }

    @Test
    void testCountByActeur() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");
        auditService.logSuccess(TypeActionAudit.USER_UPDATE, "USER", "2", "user2", "realm1", "admin", "Updated");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime past = now.minusDays(1);
        LocalDateTime future = now.plusDays(1);

        java.util.Map<String, Long> counts = auditService.countByActeur("realm1", past, future);

        assertNotNull(counts);
    }

    @Test
    void testCountSuccessVsFailure() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");
        auditService.logFailure(TypeActionAudit.USER_CREATE, "USER", "2", "user2", "realm1", "admin", "Failed", "Error");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime past = now.minusDays(1);
        LocalDateTime future = now.plusDays(1);

        java.util.Map<String, Long> result = auditService.countSuccessVsFailure("realm1", past, future);

        assertNotNull(result);
        assertTrue(result.containsKey("success"));
        assertTrue(result.containsKey("failure"));
    }

    @Test
    void testExportToCSV() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime past = now.minusDays(1);
        LocalDateTime future = now.plusDays(1);

        String csv = auditService.exportToCSV("realm1", past, future);

        assertNotNull(csv);
        assertTrue(csv.length() > 0);
    }

    @Test
    void testPurgeOldLogs() {
        // Créer des logs anciens
        for (int i = 0; i < 10; i++) {
            auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", String.valueOf(i), 
                "user" + i, "realm1", "admin", "Created");
        }

        LocalDateTime cutoffDate = LocalDateTime.now().minusDays(30);
        long purged = auditService.purgeOldLogs(cutoffDate);

        assertTrue(purged >= 0);
    }

    @Test
    void testGetTotalCount() {
        auditService.logSuccess(TypeActionAudit.USER_CREATE, "USER", "1", "user1", "realm1", "admin", "Created");
        auditService.logSuccess(TypeActionAudit.USER_UPDATE, "USER", "2", "user2", "realm1", "admin", "Updated");

        long total = auditService.getTotalCount();

        assertEquals(2, total);
    }
}

