package dev.lions.unionflow.server.entity;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.UUID;

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

@DisplayName("Notification")
class NotificationTest {

    @Test
    @DisplayName("getters/setters")
    void gettersSetters() {
        Notification n = new Notification();
        n.setTypeNotification("EMAIL");
        n.setPriorite("HAUTE");
        n.setStatut("EN_ATTENTE");
        n.setSujet("Sujet");
        n.setCorps("Corps du message");
        n.setDateEnvoiPrevue(LocalDateTime.now());
        n.setNombreTentatives(1);
        n.setDonneesAdditionnelles("{}");

        assertThat(n.getTypeNotification()).isEqualTo("EMAIL");
        assertThat(n.getPriorite()).isEqualTo("HAUTE");
        assertThat(n.getStatut()).isEqualTo("EN_ATTENTE");
        assertThat(n.getSujet()).isEqualTo("Sujet");
        assertThat(n.getCorps()).isEqualTo("Corps du message");
        assertThat(n.getNombreTentatives()).isEqualTo(1);
    }

    @Test
    @DisplayName("priorite et statut par défaut")
    void defauts() {
        Notification n = new Notification();
        n.setTypeNotification("EMAIL");
        assertThat(n.getPriorite()).isEqualTo("NORMALE");
        assertThat(n.getStatut()).isEqualTo("EN_ATTENTE");
        assertThat(n.getNombreTentatives()).isEqualTo(0);
    }

    @Test
    @DisplayName("isEnvoyee et isLue")
    void isEnvoyee_isLue() {
        Notification n = new Notification();
        n.setTypeNotification("EMAIL");
        n.setStatut("ENVOYEE");
        assertThat(n.isEnvoyee()).isTrue();
        n.setStatut("LUE");
        assertThat(n.isLue()).isTrue();
        n.setStatut("EN_ATTENTE");
        assertThat(n.isEnvoyee()).isFalse();
        assertThat(n.isLue()).isFalse();
    }

    @Test
    @DisplayName("relations membre, organisation, template")
    void relations() {
        Notification n = new Notification();
        n.setTypeNotification("EMAIL");
        Membre m = new Membre();
        Organisation o = new Organisation();
        TemplateNotification t = new TemplateNotification();
        n.setMembre(m);
        n.setOrganisation(o);
        n.setTemplate(t);
        assertThat(n.getMembre()).isSameAs(m);
        assertThat(n.getOrganisation()).isSameAs(o);
        assertThat(n.getTemplate()).isSameAs(t);
    }

    @Test
    @DisplayName("equals et hashCode")
    void equalsHashCode() {
        UUID id = UUID.randomUUID();
        Notification a = new Notification();
        a.setId(id);
        a.setTypeNotification("EMAIL");
        a.setStatut("EN_ATTENTE");
        Notification b = new Notification();
        b.setId(id);
        b.setTypeNotification("EMAIL");
        b.setStatut("EN_ATTENTE");
        assertThat(a).isEqualTo(b);
        assertThat(a.hashCode()).isEqualTo(b.hashCode());
    }

    @Test
    @DisplayName("toString non null")
    void toString_nonNull() {
        Notification n = new Notification();
        n.setTypeNotification("EMAIL");
        assertThat(n.toString()).isNotNull().isNotEmpty();
    }

    @Test
    @DisplayName("isEnvoyee: false si statut null")
    void isEnvoyee_nullStatut_returnsFalse() {
        Notification n = new Notification();
        n.setTypeNotification("EMAIL");
        n.setStatut(null);
        assertThat(n.isEnvoyee()).isFalse();
        assertThat(n.isLue()).isFalse();
    }

    @Test
    @DisplayName("onCreate: initialise les défauts si null")
    void onCreate_setsDefaults() throws Exception {
        Notification n = new Notification();
        n.setTypeNotification("EMAIL");
        n.setPriorite(null);
        n.setStatut(null);
        n.setNombreTentatives(null);
        n.setDateEnvoiPrevue(null);

        Method onCreate = Notification.class.getDeclaredMethod("onCreate");
        onCreate.setAccessible(true);
        onCreate.invoke(n);

        assertThat(n.getPriorite()).isEqualTo("NORMALE");
        assertThat(n.getStatut()).isEqualTo("EN_ATTENTE");
        assertThat(n.getNombreTentatives()).isEqualTo(0);
        assertThat(n.getDateEnvoiPrevue()).isNotNull();
    }
}
