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

import dev.lions.unionflow.server.api.enums.agricole.StatutCampagneAgricole;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.entity.agricole.CampagneAgricole;
import dev.lions.unionflow.server.repository.OrganisationRepository;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.TestTransaction;
import jakarta.inject.Inject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
class CampagneAgricoleRepositoryTest {

    @Inject
    CampagneAgricoleRepository campagneAgricoleRepository;
    @Inject
    OrganisationRepository organisationRepository;

    private Organisation newOrganisation() {
        Organisation o = new Organisation();
        o.setNom("Org Agricole");
        o.setTypeOrganisation("ASSOCIATION");
        o.setStatut("ACTIVE");
        o.setEmail("agricole-" + UUID.randomUUID() + "@test.com");
        o.setActif(true);
        organisationRepository.persist(o);
        return o;
    }

    @Test
    @TestTransaction
    @DisplayName("findById retourne null pour UUID inexistant")
    void findById_inexistant_returnsNull() {
        assertThat(campagneAgricoleRepository.findById(UUID.randomUUID())).isNull();
    }

    @Test
    @TestTransaction
    @DisplayName("listAll retourne une liste")
    void listAll_returnsList() {
        List<CampagneAgricole> list = campagneAgricoleRepository.listAll();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("count retourne un nombre >= 0")
    void count_returnsNonNegative() {
        assertThat(campagneAgricoleRepository.count()).isGreaterThanOrEqualTo(0L);
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve la campagne")
    void persist_thenFindById_findsCampagne() {
        Organisation org = newOrganisation();
        CampagneAgricole c = CampagneAgricole.builder()
                .organisation(org)
                .designation("Campagne test")
                .statut(StatutCampagneAgricole.PREPARATION)
                .build();
        campagneAgricoleRepository.persist(c);
        assertThat(c.getId()).isNotNull();
        CampagneAgricole found = campagneAgricoleRepository.findById(c.getId());
        assertThat(found).isNotNull();
        assertThat(found.getDesignation()).isEqualTo("Campagne test");
    }
}
