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("TypeReference")
class TypeReferenceTest {

    @Test
    @DisplayName("getters/setters")
    void gettersSetters() {
        TypeReference tr = new TypeReference();
        tr.setDomaine("STATUT_ORGANISATION");
        tr.setCode("ACTIVE");
        tr.setLibelle("Actif");
        tr.setDescription("Organisation active");
        tr.setOrdreAffichage(1);
        tr.setEstDefaut(true);
        tr.setEstSysteme(false);

        assertThat(tr.getDomaine()).isEqualTo("STATUT_ORGANISATION");
        assertThat(tr.getCode()).isEqualTo("ACTIVE");
        assertThat(tr.getLibelle()).isEqualTo("Actif");
        assertThat(tr.getOrdreAffichage()).isEqualTo(1);
        assertThat(tr.getEstDefaut()).isTrue();
    }

    @Test
    @DisplayName("onCreate: domaine et code non-null → normalisés en majuscules")
    void onCreate_normalisesDomaineAndCode() throws Exception {
        TypeReference tr = new TypeReference();
        tr.setDomaine("statut_org");
        tr.setCode("active");
        tr.setLibelle("Actif");
        Method onCreate = TypeReference.class.getDeclaredMethod("onCreate");
        onCreate.setAccessible(true);
        onCreate.invoke(tr);
        assertThat(tr.getDomaine()).isEqualTo("STATUT_ORG");
        assertThat(tr.getCode()).isEqualTo("ACTIVE");
    }

    @Test
    @DisplayName("onCreate: domaine null → skipped (no NPE), code null → skipped")
    void onCreate_nullDomaineAndCode_skipped() throws Exception {
        TypeReference tr = new TypeReference();
        tr.setDomaine(null);
        tr.setCode(null);
        tr.setLibelle("L");
        Method onCreate = TypeReference.class.getDeclaredMethod("onCreate");
        onCreate.setAccessible(true);
        onCreate.invoke(tr);
        assertThat(tr.getDomaine()).isNull();
        assertThat(tr.getCode()).isNull();
    }

    @Test
    @DisplayName("equals et hashCode")
    void equalsHashCode() {
        UUID id = UUID.randomUUID();
        TypeReference a = new TypeReference();
        a.setId(id);
        a.setDomaine("D");
        a.setCode("C");
        a.setLibelle("L");
        TypeReference b = new TypeReference();
        b.setId(id);
        b.setDomaine("D");
        b.setCode("C");
        b.setLibelle("L");
        assertThat(a).isEqualTo(b);
        assertThat(a.hashCode()).isEqualTo(b.hashCode());
    }

    @Test
    @DisplayName("toString non null")
    void toString_nonNull() {
        TypeReference tr = new TypeReference();
        tr.setDomaine("D");
        tr.setCode("C");
        tr.setLibelle("L");
        assertThat(tr.toString()).isNotNull().isNotEmpty();
    }
}
