package dev.lions.unionflow.server.entity;

import dev.lions.unionflow.server.api.enums.wave.StatutCompteWave;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

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

@DisplayName("CompteWave")
class CompteWaveTest {

    @Test
    @DisplayName("getters/setters")
    void gettersSetters() {
        CompteWave c = new CompteWave();
        c.setNumeroTelephone("+22507000001");
        c.setStatutCompte(StatutCompteWave.VERIFIE);
        c.setWaveAccountId("wa-1");
        c.setWaveApiKey("key");
        c.setEnvironnement("PRODUCTION");
        LocalDateTime dt = LocalDateTime.now();
        c.setDateDerniereVerification(dt);
        c.setCommentaire("OK");

        assertThat(c.getNumeroTelephone()).isEqualTo("+22507000001");
        assertThat(c.getStatutCompte()).isEqualTo(StatutCompteWave.VERIFIE);
        assertThat(c.getWaveAccountId()).isEqualTo("wa-1");
        assertThat(c.getWaveApiKey()).isEqualTo("key");
        assertThat(c.getEnvironnement()).isEqualTo("PRODUCTION");
        assertThat(c.getDateDerniereVerification()).isEqualTo(dt);
        assertThat(c.getCommentaire()).isEqualTo("OK");
    }

    @Test
    @DisplayName("statut par défaut NON_VERIFIE")
    void statutDefaut() {
        CompteWave c = new CompteWave();
        c.setNumeroTelephone("+22507000002");
        assertThat(c.getStatutCompte()).isEqualTo(StatutCompteWave.NON_VERIFIE);
    }

    @Test
    @DisplayName("relations organisation et membre")
    void relations() {
        CompteWave c = new CompteWave();
        c.setNumeroTelephone("+22507000003");
        Organisation o = new Organisation();
        Membre m = new Membre();
        c.setOrganisation(o);
        c.setMembre(m);
        assertThat(c.getOrganisation()).isSameAs(o);
        assertThat(c.getMembre()).isSameAs(m);
    }

    @Test
    @DisplayName("equals et hashCode")
    void equalsHashCode() {
        UUID id = UUID.randomUUID();
        CompteWave a = new CompteWave();
        a.setId(id);
        a.setNumeroTelephone("+22507000004");
        CompteWave b = new CompteWave();
        b.setId(id);
        b.setNumeroTelephone("+22507000004");
        assertThat(a).isEqualTo(b);
        assertThat(a.hashCode()).isEqualTo(b.hashCode());
    }

    @Test
    @DisplayName("isVerifie et peutEtreUtilise true quand VERIFIE")
    void isVerifie_peutEtreUtilise() {
        CompteWave c = new CompteWave();
        c.setNumeroTelephone("+22507000006");
        c.setStatutCompte(StatutCompteWave.VERIFIE);
        assertThat(c.isVerifie()).isTrue();
        assertThat(c.peutEtreUtilise()).isTrue();
        c.setStatutCompte(StatutCompteWave.NON_VERIFIE);
        assertThat(c.isVerifie()).isFalse();
        assertThat(c.peutEtreUtilise()).isFalse();
    }

    @Test
    @DisplayName("toString non null")
    void toString_nonNull() {
        CompteWave c = new CompteWave();
        c.setNumeroTelephone("+22507000005");
        assertThat(c.toString()).isNotNull().isNotEmpty();
    }

    @Test
    @DisplayName("onCreate: environnement null → SANDBOX")
    void onCreate_environnementNull_defaultsSandbox() {
        CompteWave c = new CompteWave();
        c.setStatutCompte(null);
        c.setEnvironnement(null);
        c.onCreate();
        assertThat(c.getEnvironnement()).isEqualTo("SANDBOX");
        assertThat(c.getStatutCompte()).isEqualTo(StatutCompteWave.NON_VERIFIE);
    }

    @Test
    @DisplayName("onCreate: environnement vide → SANDBOX")
    void onCreate_environnementEmpty_defaultsSandbox() {
        CompteWave c = new CompteWave();
        c.setEnvironnement("");
        c.onCreate();
        assertThat(c.getEnvironnement()).isEqualTo("SANDBOX");
    }

    @Test
    @DisplayName("onCreate: statutCompte et environnement déjà définis → conservés (branches false)")
    void onCreate_allDejaDefinis_conservesValues() {
        // Valeurs déjà définies → conservées sans modification
        CompteWave c = new CompteWave();
        c.setStatutCompte(StatutCompteWave.VERIFIE);
        c.setEnvironnement("PRODUCTION");

        c.onCreate();

        assertThat(c.getStatutCompte()).isEqualTo(StatutCompteWave.VERIFIE);
        assertThat(c.getEnvironnement()).isEqualTo("PRODUCTION");
    }
}
