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

import dev.lions.unionflow.server.api.enums.mutuelle.epargne.StatutCompteEpargne;
import dev.lions.unionflow.server.api.enums.mutuelle.epargne.TypeCompteEpargne;
import dev.lions.unionflow.server.api.enums.mutuelle.epargne.TypeTransactionEpargne;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.entity.mutuelle.epargne.CompteEpargne;
import dev.lions.unionflow.server.entity.mutuelle.epargne.TransactionEpargne;
import dev.lions.unionflow.server.repository.MembreRepository;
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.math.BigDecimal;
import java.util.List;
import java.util.UUID;

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

@QuarkusTest
class TransactionEpargneRepositoryTest {

    @Inject
    TransactionEpargneRepository transactionEpargneRepository;
    @Inject
    CompteEpargneRepository compteEpargneRepository;
    @Inject
    OrganisationRepository organisationRepository;
    @Inject
    MembreRepository membreRepository;

    private CompteEpargne newCompteEpargne() {
        Organisation o = new Organisation();
        o.setNom("Org Tx Epargne");
        o.setTypeOrganisation("ASSOCIATION");
        o.setStatut("ACTIVE");
        o.setEmail("tx-ep-" + UUID.randomUUID() + "@test.com");
        o.setActif(true);
        organisationRepository.persist(o);
        Membre m = new Membre();
        m.setNumeroMembre("TX-" + UUID.randomUUID().toString().substring(0, 8));
        m.setPrenom("Test");
        m.setNom("User");
        m.setEmail("tx-m-" + UUID.randomUUID() + "@test.com");
        m.setDateNaissance(java.time.LocalDate.of(1990, 1, 1));
        m.setActif(true);
        membreRepository.persist(m);
        CompteEpargne c = CompteEpargne.builder()
                .membre(m)
                .organisation(o)
                .numeroCompte("TX-CEP-" + UUID.randomUUID().toString().substring(0, 8))
                .typeCompte(TypeCompteEpargne.COURANT)
                .statut(StatutCompteEpargne.ACTIF)
                .build();
        compteEpargneRepository.persist(c);
        return c;
    }

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

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

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

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve la transaction")
    void persist_thenFindById_findsTransaction() {
        CompteEpargne compte = newCompteEpargne();
        TransactionEpargne t = TransactionEpargne.builder()
                .compte(compte)
                .type(TypeTransactionEpargne.DEPOT)
                .montant(BigDecimal.valueOf(5000))
                .build();
        transactionEpargneRepository.persist(t);
        assertThat(t.getId()).isNotNull();
        TransactionEpargne found = transactionEpargneRepository.findById(t.getId());
        assertThat(found).isNotNull();
        assertThat(found.getMontant()).isEqualByComparingTo(BigDecimal.valueOf(5000));
    }
}
