package dev.lions.unionflow.server.scheduler;

import dev.lions.unionflow.server.service.MemberLifecycleService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class MemberLifecycleSchedulerTest {

    @InjectMocks
    MemberLifecycleScheduler scheduler;

    @Mock
    MemberLifecycleService lifecycleService;

    @Test
    void rappelerInvitations_whenCountPositive_logs() {
        when(lifecycleService.envoyerRappelsInvitation()).thenReturn(3);

        assertThatCode(() -> scheduler.rappelerInvitationsExpirantBientot())
                .doesNotThrowAnyException();

        verify(lifecycleService).envoyerRappelsInvitation();
    }

    @Test
    void rappelerInvitations_whenCountZero_noLog() {
        when(lifecycleService.envoyerRappelsInvitation()).thenReturn(0);

        assertThatCode(() -> scheduler.rappelerInvitationsExpirantBientot())
                .doesNotThrowAnyException();

        verify(lifecycleService).envoyerRappelsInvitation();
    }

    @Test
    void rappelerInvitations_whenServiceThrows_doesNotPropagate() {
        when(lifecycleService.envoyerRappelsInvitation()).thenThrow(new RuntimeException("DB down"));

        assertThatCode(() -> scheduler.rappelerInvitationsExpirantBientot())
                .doesNotThrowAnyException();
    }

    @Test
    void expirerInvitations_whenCountPositive_logs() {
        when(lifecycleService.expirerInvitations()).thenReturn(5);

        assertThatCode(() -> scheduler.expirerInvitations())
                .doesNotThrowAnyException();

        verify(lifecycleService).expirerInvitations();
    }

    @Test
    void expirerInvitations_whenCountZero_noLog() {
        when(lifecycleService.expirerInvitations()).thenReturn(0);

        assertThatCode(() -> scheduler.expirerInvitations())
                .doesNotThrowAnyException();

        verify(lifecycleService).expirerInvitations();
    }

    @Test
    void expirerInvitations_whenServiceThrows_doesNotPropagate() {
        when(lifecycleService.expirerInvitations()).thenThrow(new RuntimeException("Timeout"));

        assertThatCode(() -> scheduler.expirerInvitations())
                .doesNotThrowAnyException();
    }
}
