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

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.agricole.CampagneAgricoleDTO;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.entity.agricole.CampagneAgricole;
import dev.lions.unionflow.server.mapper.agricole.CampagneAgricoleMapper;
import dev.lions.unionflow.server.repository.OrganisationRepository;
import dev.lions.unionflow.server.repository.agricole.CampagneAgricoleRepository;
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 CampagneAgricoleServiceTest {

    @Inject
    CampagneAgricoleService service;

    @InjectMock
    CampagneAgricoleRepository repository;

    @InjectMock
    OrganisationRepository organisationRepository;

    @InjectMock
    CampagneAgricoleMapper mapper;

    @Test
    @DisplayName("creerCampagne lie l'organisation et persiste la campagne")
    void creerCampagne_success() {
        UUID orgId = UUID.randomUUID();
        CampagneAgricoleDTO dto = new CampagneAgricoleDTO();
        dto.setOrganisationCoopId(orgId.toString());

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

        CampagneAgricole entity = new CampagneAgricole();

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

        CampagneAgricoleDTO result = service.creerCampagne(dto);

        assertThat(result).isNotNull();
        verify(repository).persist(any(CampagneAgricole.class));
        assertThat(entity.getOrganisation()).isEqualTo(org);
    }

    @Test
    @DisplayName("creerCampagne lance NotFoundException si l'organisation n'existe pas")
    void creerCampagne_orgNotFound_throwsNotFound() {
        UUID orgId = UUID.randomUUID();
        CampagneAgricoleDTO dto = new CampagneAgricoleDTO();
        dto.setOrganisationCoopId(orgId.toString());

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

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

    @Test
    @DisplayName("getCampagneById retourne le DTO quand la campagne existe")
    void getCampagneById_found_returnsDto() {
        UUID id = UUID.randomUUID();
        CampagneAgricole entity = new CampagneAgricole();
        CampagneAgricoleDTO dto = new CampagneAgricoleDTO();

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

        CampagneAgricoleDTO result = service.getCampagneById(id);

        assertThat(result).isEqualTo(dto);
    }

    @Test
    @DisplayName("getCampagneById lance NotFoundException si la campagne n'existe pas")
    void getCampagneById_notFound_throws() {
        UUID id = UUID.randomUUID();
        when(repository.findByIdOptional(id)).thenReturn(Optional.empty());

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

    @Test
    @SuppressWarnings("unchecked")
    @DisplayName("getCampagnesByCooperative retourne la liste des campagnes")
    void getCampagnesByCooperative_returnsList() {
        UUID orgId = UUID.randomUUID();
        CampagneAgricole entity = new CampagneAgricole();
        CampagneAgricoleDTO dto = new CampagneAgricoleDTO();

        PanacheQuery<CampagneAgricole> 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<CampagneAgricoleDTO> result = service.getCampagnesByCooperative(orgId);

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