package dev.lions.unionflow.server.dto;

import dev.lions.unionflow.server.entity.Evenement;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.entity.Organisation;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

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

@DisplayName("EvenementMobileDTO")
class EvenementMobileDTOTest {

    @Test
    @DisplayName("fromEntity: retourne null si evenement null")
    void fromEntity_null_returnsNull() {
        assertThat(EvenementMobileDTO.fromEntity(null)).isNull();
    }

    @Test
    @DisplayName("fromEntity: convertit correctement une entité Evenement")
    void fromEntity_validEntity_mapsAllFields() {
        Evenement e = new Evenement();
        e.setId(UUID.randomUUID());
        e.setTitre("AG 2025");
        e.setDescription("Assemblée générale");
        e.setDateDebut(LocalDateTime.of(2025, 6, 1, 10, 0));
        e.setDateFin(LocalDateTime.of(2025, 6, 1, 12, 0));
        e.setLieu("Salle A");
        e.setTypeEvenement("ASSEMBLEE_GENERALE");
        e.setStatut("PLANIFIE");
        e.setCapaciteMax(100);
        e.setPrix(new BigDecimal("5000.00"));
        e.setVisiblePublic(true);
        e.setInscriptionRequise(true);
        e.setActif(true);

        Membre organisateur = new Membre();
        organisateur.setId(UUID.randomUUID());
        organisateur.setNom("Diallo");
        organisateur.setPrenom("Amadou");
        e.setOrganisateur(organisateur);

        Organisation org = new Organisation();
        org.setId(UUID.randomUUID());
        org.setNom("Lions Club");
        e.setOrganisation(org);

        EvenementMobileDTO dto = EvenementMobileDTO.fromEntity(e);

        assertThat(dto).isNotNull();
        assertThat(dto.getTitre()).isEqualTo("AG 2025");
        assertThat(dto.getStatut()).isEqualTo("PLANIFIE");
        assertThat(dto.getMaxParticipants()).isEqualTo(100);
        assertThat(dto.getOrganisateurId()).isEqualTo(organisateur.getId());
        assertThat(dto.getOrganisationId()).isEqualTo(org.getId());
        assertThat(dto.getOrganisationNom()).isEqualTo("Lions Club");
        assertThat(dto.getEstPublic()).isTrue();
        assertThat(dto.getCout()).isEqualByComparingTo("5000.00");
        assertThat(dto.getDevise()).isEqualTo("XOF");
        assertThat(dto.getPriorite()).isEqualTo("MOYENNE");
        assertThat(dto.getTags()).isNotNull();
    }

    @Test
    @DisplayName("fromEntity: null organisateur et organisation → IDs null")
    void fromEntity_nullRelations_idsNull() {
        Evenement e = new Evenement();
        e.setTitre("Ev");
        e.setDateDebut(LocalDateTime.now());
        e.setStatut("PLANIFIE");
        e.setOrganisateur(null);
        e.setOrganisation(null);

        EvenementMobileDTO dto = EvenementMobileDTO.fromEntity(e);

        assertThat(dto).isNotNull();
        assertThat(dto.getOrganisateurId()).isNull();
        assertThat(dto.getOrganisateurNom()).isNull();
        assertThat(dto.getOrganisationId()).isNull();
        assertThat(dto.getOrganisationNom()).isNull();
    }

    @Test
    @DisplayName("fromEntity: typeEvenement null → type null")
    void fromEntity_nullTypeEvenement_typeNull() {
        Evenement e = new Evenement();
        e.setTitre("Ev");
        e.setDateDebut(LocalDateTime.now());
        e.setTypeEvenement(null);

        EvenementMobileDTO dto = EvenementMobileDTO.fromEntity(e);

        assertThat(dto.getType()).isNull();
    }

    @Test
    @DisplayName("fromEntity: statut null → statut PLANIFIE")
    void fromEntity_nullStatut_defaultsPlanifie() {
        Evenement e = new Evenement();
        e.setTitre("Ev");
        e.setDateDebut(LocalDateTime.now());
        e.setStatut(null);

        EvenementMobileDTO dto = EvenementMobileDTO.fromEntity(e);

        assertThat(dto.getStatut()).isEqualTo("PLANIFIE");
    }

    @Test
    @DisplayName("getters/setters")
    void gettersSetters() {
        EvenementMobileDTO dto = EvenementMobileDTO.builder()
            .id(UUID.randomUUID())
            .titre("Test")
            .statut("CONFIRME")
            .build();
        assertThat(dto.getTitre()).isEqualTo("Test");
        assertThat(dto.getStatut()).isEqualTo("CONFIRME");
    }
}
