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.util.UUID;

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

@DisplayName("TemplateNotification")
class TemplateNotificationTest {

    @Test
    @DisplayName("getters/setters")
    void gettersSetters() {
        TemplateNotification t = new TemplateNotification();
        t.setCode("WELCOME");
        t.setSujet("Bienvenue");
        t.setCorpsTexte("Bonjour {{nom}}");
        t.setCorpsHtml("<p>Bonjour {{nom}}</p>");
        t.setLangue("fr");
        t.setDescription("Template de bienvenue");

        assertThat(t.getCode()).isEqualTo("WELCOME");
        assertThat(t.getSujet()).isEqualTo("Bienvenue");
        assertThat(t.getLangue()).isEqualTo("fr");
    }

    @Test
    @DisplayName("onCreate: langue null → initialisée à 'fr'")
    void onCreate_langue_null_setsDefault() throws Exception {
        TemplateNotification t = new TemplateNotification();
        t.setCode("T1");
        t.setLangue(null);
        Method onCreate = TemplateNotification.class.getDeclaredMethod("onCreate");
        onCreate.setAccessible(true);
        onCreate.invoke(t);
        assertThat(t.getLangue()).isEqualTo("fr");
    }

    @Test
    @DisplayName("onCreate: langue vide → initialisée à 'fr'")
    void onCreate_langue_empty_setsDefault() throws Exception {
        TemplateNotification t = new TemplateNotification();
        t.setCode("T2");
        t.setLangue("");
        Method onCreate = TemplateNotification.class.getDeclaredMethod("onCreate");
        onCreate.setAccessible(true);
        onCreate.invoke(t);
        assertThat(t.getLangue()).isEqualTo("fr");
    }

    @Test
    @DisplayName("onCreate: langue déjà renseignée → non écrasée")
    void onCreate_langue_alreadySet_notOverwritten() throws Exception {
        TemplateNotification t = new TemplateNotification();
        t.setCode("T3");
        t.setLangue("en");
        Method onCreate = TemplateNotification.class.getDeclaredMethod("onCreate");
        onCreate.setAccessible(true);
        onCreate.invoke(t);
        assertThat(t.getLangue()).isEqualTo("en");
    }

    @Test
    @DisplayName("equals et hashCode")
    void equalsHashCode() {
        UUID id = UUID.randomUUID();
        TemplateNotification a = new TemplateNotification();
        a.setId(id);
        a.setCode("C1");
        a.setSujet("S1");
        TemplateNotification b = new TemplateNotification();
        b.setId(id);
        b.setCode("C1");
        b.setSujet("S1");
        assertThat(a).isEqualTo(b);
        assertThat(a.hashCode()).isEqualTo(b.hashCode());
    }

    @Test
    @DisplayName("toString non null")
    void toString_nonNull() {
        TemplateNotification t = new TemplateNotification();
        t.setCode("X");
        t.setSujet("Y");
        assertThat(t.toString()).isNotNull().isNotEmpty();
    }
}
