package dev.lions.unionflow.server.entity;

import dev.lions.unionflow.server.api.enums.messagerie.TypePolitiqueCommunication;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.UUID;

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

@DisplayName("ContactPolicy")
class ContactPolicyTest {

    private static Organisation newOrg() {
        Organisation org = new Organisation();
        org.setId(UUID.randomUUID());
        org.setNom("Org Test");
        return org;
    }

    @Test
    @DisplayName("getters/setters — tous les champs")
    void gettersSetters() {
        ContactPolicy policy = new ContactPolicy();
        Organisation org = newOrg();
        policy.setOrganisation(org);
        policy.setTypePolitique(TypePolitiqueCommunication.BUREAU_SEULEMENT);
        policy.setAutoriserMembreVersMembre(false);
        policy.setAutoriserMembreVersRole(true);
        policy.setAutoriserNotesVocales(false);

        assertThat(policy.getOrganisation()).isEqualTo(org);
        assertThat(policy.getTypePolitique()).isEqualTo(TypePolitiqueCommunication.BUREAU_SEULEMENT);
        assertThat(policy.getAutoriserMembreVersMembre()).isFalse();
        assertThat(policy.getAutoriserMembreVersRole()).isTrue();
        assertThat(policy.getAutoriserNotesVocales()).isFalse();
    }

    @Test
    @DisplayName("onCreate — valeurs par défaut : OUVERT, tout à true")
    void onCreate_defaults() {
        ContactPolicy policy = new ContactPolicy();
        policy.setOrganisation(newOrg());

        policy.onCreate();

        assertThat(policy.getTypePolitique()).isEqualTo(TypePolitiqueCommunication.OUVERT);
        assertThat(policy.getAutoriserMembreVersMembre()).isTrue();
        assertThat(policy.getAutoriserMembreVersRole()).isTrue();
        assertThat(policy.getAutoriserNotesVocales()).isTrue();
        assertThat(policy.getDateCreation()).isNotNull();
        assertThat(policy.getActif()).isTrue();
    }

    @Test
    @DisplayName("onCreate — ne remplace pas les valeurs existantes")
    void onCreate_preservesExisting() {
        ContactPolicy policy = new ContactPolicy();
        policy.setOrganisation(newOrg());
        policy.setTypePolitique(TypePolitiqueCommunication.GROUPES_INTERNES);
        policy.setAutoriserNotesVocales(false);

        policy.onCreate();

        assertThat(policy.getTypePolitique()).isEqualTo(TypePolitiqueCommunication.GROUPES_INTERNES);
        assertThat(policy.getAutoriserNotesVocales()).isFalse();
    }

    @Test
    @DisplayName("builder — tous les champs")
    void builder_allFields() {
        Organisation org = newOrg();
        ContactPolicy policy = ContactPolicy.builder()
            .organisation(org)
            .typePolitique(TypePolitiqueCommunication.OUVERT)
            .autoriserMembreVersMembre(true)
            .autoriserMembreVersRole(true)
            .autoriserNotesVocales(true)
            .build();

        assertThat(policy.getOrganisation()).isEqualTo(org);
        assertThat(policy.getTypePolitique()).isEqualTo(TypePolitiqueCommunication.OUVERT);
        assertThat(policy.getAutoriserNotesVocales()).isTrue();
    }

    @Test
    @DisplayName("equals et hashCode")
    void equalsHashCode() {
        UUID id = UUID.randomUUID();
        Organisation org = newOrg();

        ContactPolicy a = ContactPolicy.builder().organisation(org)
            .typePolitique(TypePolitiqueCommunication.OUVERT).build();
        a.setId(id);
        ContactPolicy b = ContactPolicy.builder().organisation(org)
            .typePolitique(TypePolitiqueCommunication.OUVERT).build();
        b.setId(id);

        assertThat(a).isEqualTo(b);
        assertThat(a.hashCode()).isEqualTo(b.hashCode());
    }

    @Test
    @DisplayName("toString non null")
    void toString_nonNull() {
        ContactPolicy policy = ContactPolicy.builder().organisation(newOrg()).build();
        assertThat(policy.toString()).isNotNull().isNotEmpty();
    }
}
