package dev.lions.unionflow.server.repository.mutuelle;

import dev.lions.unionflow.server.entity.mutuelle.ParametresFinanciersMutuelle;
import io.quarkus.hibernate.orm.panache.PanacheQuery;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Optional;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@DisplayName("ParametresFinanciersMutuellRepository — tests unitaires")
class ParametresFinanciersMutuellRepositoryTest {

    static class TestableParametresFinanciersMutuellRepository
            extends ParametresFinanciersMutuellRepository {
        // instanciation sans CDI
    }

    private ParametresFinanciersMutuellRepository repo;

    @BeforeEach
    void setUp() {
        repo = spy(new TestableParametresFinanciersMutuellRepository());
    }

    @Test
    @DisplayName("classExists : instanciation possible sans CDI")
    void classExists() {
        assertThat(new TestableParametresFinanciersMutuellRepository()).isNotNull();
    }

    // ─── findByOrganisation ───────────────────────────────────────────────────

    @Test
    @DisplayName("findByOrganisation : retourne Optional.empty() si aucun paramètre pour cette organisation")
    @SuppressWarnings("unchecked")
    void findByOrganisation_noResult_returnsEmpty() {
        PanacheQuery<ParametresFinanciersMutuelle> query = mock(PanacheQuery.class);
        when(query.firstResultOptional()).thenReturn(Optional.empty());
        doReturn(query).when(repo).find(anyString(), any(UUID.class));

        Optional<ParametresFinanciersMutuelle> result =
                repo.findByOrganisation(UUID.randomUUID());

        assertThat(result).isEmpty();
    }

    @Test
    @DisplayName("findByOrganisation : retourne les paramètres financiers de l'organisation")
    @SuppressWarnings("unchecked")
    void findByOrganisation_withResult_returnsParametres() {
        ParametresFinanciersMutuelle parametres = ParametresFinanciersMutuelle.builder().build();
        PanacheQuery<ParametresFinanciersMutuelle> query = mock(PanacheQuery.class);
        when(query.firstResultOptional()).thenReturn(Optional.of(parametres));
        doReturn(query).when(repo).find(anyString(), any(UUID.class));

        Optional<ParametresFinanciersMutuelle> result =
                repo.findByOrganisation(UUID.randomUUID());

        assertThat(result).isPresent();
        assertThat(result.get()).isSameAs(parametres);
    }

    @Test
    @DisplayName("findByOrganisation : UUID null produit un appel avec null")
    @SuppressWarnings("unchecked")
    void findByOrganisation_nullId_stillCallsFind() {
        PanacheQuery<ParametresFinanciersMutuelle> query = mock(PanacheQuery.class);
        when(query.firstResultOptional()).thenReturn(Optional.empty());
        doReturn(query).when(repo).find(anyString(), (Object) isNull());

        Optional<ParametresFinanciersMutuelle> result = repo.findByOrganisation(null);

        assertThat(result).isEmpty();
    }
}
