package dev.lions.unionflow.server.service;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

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

/**
 * Tests unitaires pour FirebasePushService.
 * Firebase n'est pas initialisé (serviceAccountKeyPath vide) → tests du comportement
 * when disabled.
 */
@ExtendWith(MockitoExtension.class)
class FirebasePushServiceTest {

    private FirebasePushService service;

    @BeforeEach
    void setup() throws Exception {
        service = new FirebasePushService();
        // Injecter une clé vide pour simuler Firebase désactivé
        Field keyField = FirebasePushService.class.getDeclaredField("serviceAccountKeyPath");
        keyField.setAccessible(true);
        keyField.set(service, "");
        service.init();
    }

    @Test
    void isAvailable_returnsFalse_whenNotInitialized() {
        assertThat(service.isAvailable()).isFalse();
    }

    @Test
    void envoyerNotification_returnsFalse_whenNotInitialized() {
        boolean result = service.envoyerNotification("someToken", "titre", "corps", Map.of());
        assertThat(result).isFalse();
    }

    @Test
    void envoyerNotification_returnsFalse_whenTokenNull() {
        boolean result = service.envoyerNotification(null, "titre", "corps", null);
        assertThat(result).isFalse();
    }

    @Test
    void envoyerNotification_returnsFalse_whenTokenBlank() {
        boolean result = service.envoyerNotification("   ", "titre", "corps", null);
        assertThat(result).isFalse();
    }

    @Test
    void envoyerNotificationMulticast_returnsZero_whenNotInitialized() {
        int count = service.envoyerNotificationMulticast(List.of("t1", "t2"), "titre", "corps", null);
        assertThat(count).isZero();
    }

    @Test
    void envoyerNotificationMulticast_returnsZero_whenTokensNull() {
        int count = service.envoyerNotificationMulticast(null, "titre", "corps", null);
        assertThat(count).isZero();
    }

    @Test
    void envoyerNotificationMulticast_returnsZero_whenTokensEmpty() {
        int count = service.envoyerNotificationMulticast(List.of(), "titre", "corps", null);
        assertThat(count).isZero();
    }

    @Test
    void init_logsAndDoesNotThrow_whenKeyPathBlank() {
        // Vérifié implicitement par le @BeforeEach : pas d'exception levée
        assertThat(service.isAvailable()).isFalse();
    }

    @Test
    void init_doesNotThrow_whenFileNotFound() throws Exception {
        FirebasePushService svc = new FirebasePushService();
        Field keyField = FirebasePushService.class.getDeclaredField("serviceAccountKeyPath");
        keyField.setAccessible(true);
        keyField.set(svc, "/nonexistent/path/firebase.json");
        // Ne doit pas lever d'exception — juste logger un warning
        svc.init();
        assertThat(svc.isAvailable()).isFalse();
    }
}
