package dev.lions.unionflow.server.entity;

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

import java.time.LocalDateTime;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class RoleDelegationTest {

  private RoleDelegation buildDelegation(String statut, LocalDateTime debut, LocalDateTime fin) {
    return RoleDelegation.builder()
        .organisationId(UUID.randomUUID())
        .delegantUserId(UUID.randomUUID())
        .delegataireUserId(UUID.randomUUID())
        .roleDelegue("TRESORIER")
        .dateDebut(debut)
        .dateFin(fin)
        .statut(statut)
        .build();
  }

  @Test
  @DisplayName("isActiveAt : statut ACTIVE entre debut et fin → true")
  void activeWithinPeriod() {
    LocalDateTime now = LocalDateTime.now();
    RoleDelegation d = buildDelegation("ACTIVE", now.minusDays(1), now.plusDays(1));
    assertThat(d.isActiveAt(now)).isTrue();
  }

  @Test
  @DisplayName("isActiveAt : avant debut → false")
  void notActiveBeforeStart() {
    LocalDateTime now = LocalDateTime.now();
    RoleDelegation d = buildDelegation("ACTIVE", now.plusDays(1), now.plusDays(10));
    assertThat(d.isActiveAt(now)).isFalse();
  }

  @Test
  @DisplayName("isActiveAt : après fin → false")
  void notActiveAfterEnd() {
    LocalDateTime now = LocalDateTime.now();
    RoleDelegation d = buildDelegation("ACTIVE", now.minusDays(10), now.minusDays(1));
    assertThat(d.isActiveAt(now)).isFalse();
  }

  @Test
  @DisplayName("isActiveAt : statut REVOQUEE → false même dans la période")
  void notActiveIfRevoked() {
    LocalDateTime now = LocalDateTime.now();
    RoleDelegation d = buildDelegation("REVOQUEE", now.minusDays(1), now.plusDays(1));
    assertThat(d.isActiveAt(now)).isFalse();
  }

  @Test
  @DisplayName("isActiveAt : statut EXPIREE → false")
  void notActiveIfExpired() {
    LocalDateTime now = LocalDateTime.now();
    RoleDelegation d = buildDelegation("EXPIREE", now.minusDays(1), now.plusDays(1));
    assertThat(d.isActiveAt(now)).isFalse();
  }
}
