package de.effigenix.application.shared;

import de.effigenix.shared.common.Country;
import de.effigenix.shared.common.CountryRepository;
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 java.util.List;

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

@ExtendWith(MockitoExtension.class)
class ListCountriesTest {

    @Mock
    private CountryRepository countryRepository;

    @InjectMocks
    private ListCountries listCountries;

    @Test
    void shouldDelegateToFindAllWhenQueryIsNull() {
        var expected = List.of(new Country("DE", "Deutschland"));
        when(countryRepository.findAll()).thenReturn(expected);

        var result = listCountries.execute(null);

        assertThat(result).isEqualTo(expected);
        verify(countryRepository).findAll();
    }

    @Test
    void shouldDelegateToFindAllWhenQueryIsBlank() {
        var expected = List.of(new Country("DE", "Deutschland"));
        when(countryRepository.findAll()).thenReturn(expected);

        var result = listCountries.execute("  ");

        assertThat(result).isEqualTo(expected);
        verify(countryRepository).findAll();
    }

    @Test
    void shouldDelegateToSearchWhenQueryIsProvided() {
        var expected = List.of(new Country("DE", "Deutschland"));
        when(countryRepository.search("deutsch")).thenReturn(expected);

        var result = listCountries.execute("deutsch");

        assertThat(result).isEqualTo(expected);
        verify(countryRepository).search("deutsch");
    }

    @Test
    void shouldReturnEmptyListWhenNoMatch() {
        when(countryRepository.search("xyz")).thenReturn(List.of());

        var result = listCountries.execute("xyz");

        assertThat(result).isEmpty();
    }

    @Test
    void shouldDelegateToFindAllWhenQueryIsEmpty() {
        var expected = List.of(new Country("DE", "Deutschland"));
        when(countryRepository.findAll()).thenReturn(expected);

        var result = listCountries.execute("");

        assertThat(result).isEqualTo(expected);
        verify(countryRepository).findAll();
    }
}
