package dev.lions.unionflow.server.service.ong;

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

import dev.lions.unionflow.server.api.dto.ong.ProjetOngDTO;
import dev.lions.unionflow.server.api.enums.ong.StatutProjetOng;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.entity.ong.ProjetOng;
import dev.lions.unionflow.server.mapper.ong.ProjetOngMapper;
import dev.lions.unionflow.server.repository.OrganisationRepository;
import dev.lions.unionflow.server.repository.ong.ProjetOngRepository;
import io.quarkus.hibernate.orm.panache.PanacheQuery;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import jakarta.ws.rs.NotFoundException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@QuarkusTest
class ProjetOngServiceTest {

    @Inject
    ProjetOngService service;

    @InjectMock
    ProjetOngRepository repository;

    @InjectMock
    OrganisationRepository organisationRepository;

    @InjectMock
    ProjetOngMapper mapper;

    @Test
    @DisplayName("creerProjet initialise le statut à EN_ETUDE")
    void creerProjet_success() {
        UUID orgId = UUID.randomUUID();
        ProjetOngDTO dto = new ProjetOngDTO();
        dto.setOrganisationId(orgId.toString());

        Organisation org = new Organisation();
        org.setId(orgId);

        ProjetOng entity = new ProjetOng();

        when(organisationRepository.findByIdOptional(orgId)).thenReturn(Optional.of(org));
        when(mapper.toEntity(dto)).thenReturn(entity);
        when(mapper.toDto(entity)).thenReturn(dto);

        service.creerProjet(dto);

        assertThat(entity.getStatut()).isEqualTo(StatutProjetOng.EN_ETUDE);
        assertThat(entity.getOrganisation()).isEqualTo(org);
        verify(repository).persist(any(ProjetOng.class));
    }

    @Test
    @DisplayName("creerProjet lance NotFoundException si l'organisation n'existe pas")
    void creerProjet_orgNotFound_throwsNotFound() {
        UUID orgId = UUID.randomUUID();
        ProjetOngDTO dto = new ProjetOngDTO();
        dto.setOrganisationId(orgId.toString());

        when(organisationRepository.findByIdOptional(orgId)).thenReturn(Optional.empty());

        assertThatThrownBy(() -> service.creerProjet(dto))
                .isInstanceOf(NotFoundException.class);
    }

    @Test
    @DisplayName("getProjetById retourne le DTO quand le projet existe")
    void getProjetById_found_returnsDto() {
        UUID id = UUID.randomUUID();
        ProjetOng entity = new ProjetOng();
        ProjetOngDTO dto = new ProjetOngDTO();

        when(repository.findByIdOptional(id)).thenReturn(Optional.of(entity));
        when(mapper.toDto(entity)).thenReturn(dto);

        ProjetOngDTO result = service.getProjetById(id);

        assertThat(result).isEqualTo(dto);
    }

    @Test
    @DisplayName("getProjetById lance NotFoundException si le projet n'existe pas")
    void getProjetById_notFound_throws() {
        UUID id = UUID.randomUUID();
        when(repository.findByIdOptional(id)).thenReturn(Optional.empty());

        assertThatThrownBy(() -> service.getProjetById(id))
                .isInstanceOf(NotFoundException.class);
    }

    @Test
    @SuppressWarnings("unchecked")
    @DisplayName("getProjetsByOng retourne la liste des projets")
    void getProjetsByOng_returnsList() {
        UUID orgId = UUID.randomUUID();
        ProjetOng entity = new ProjetOng();
        ProjetOngDTO dto = new ProjetOngDTO();

        PanacheQuery<ProjetOng> mockQuery = mock(PanacheQuery.class);
        when(repository.find(anyString(), (Object[]) any())).thenReturn(mockQuery);
        when(mockQuery.stream()).thenReturn(List.of(entity).stream());
        when(mapper.toDto(entity)).thenReturn(dto);

        List<ProjetOngDTO> result = service.getProjetsByOng(orgId);

        assertThat(result).hasSize(1).containsExactly(dto);
    }

    @Test
    @DisplayName("changerStatut met à jour le statut du projet")
    void changerStatut_found_success() {
        UUID id = UUID.randomUUID();
        ProjetOng entity = new ProjetOng();
        ProjetOngDTO dto = new ProjetOngDTO();

        when(repository.findByIdOptional(id)).thenReturn(Optional.of(entity));
        when(mapper.toDto(entity)).thenReturn(dto);

        ProjetOngDTO result = service.changerStatut(id, StatutProjetOng.EN_COURS);

        assertThat(entity.getStatut()).isEqualTo(StatutProjetOng.EN_COURS);
        assertThat(result).isEqualTo(dto);
    }

    @Test
    @DisplayName("changerStatut lance NotFoundException si le projet n'existe pas")
    void changerStatut_notFound_throws() {
        UUID id = UUID.randomUUID();
        when(repository.findByIdOptional(id)).thenReturn(Optional.empty());

        assertThatThrownBy(() -> service.changerStatut(id, StatutProjetOng.EN_COURS))
                .isInstanceOf(NotFoundException.class);
    }
}
