package dev.lions.unionflow.client.view;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import dev.lions.unionflow.client.api.dto.ComplianceSnapshotDto;
import dev.lions.unionflow.client.api.dto.ComplianceSnapshotDto.ConformiteIndicateurDto;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
 * Tests purs logique du bean conformité — pas d'invocation REST réelle.
 *
 * @since 2026-04-25 (Sprint 8)
 */
class ConformiteDashboardBeanTest {

  private ConformiteDashboardBean bean;

  @BeforeEach
  void setUp() {
    bean = new ConformiteDashboardBean();
  }

  private void setSnapshot(ConformiteDashboardBean b, ComplianceSnapshotDto s) throws Exception {
    Field f = ConformiteDashboardBean.class.getDeclaredField("snapshot");
    f.setAccessible(true);
    f.set(b, s);
  }

  private ComplianceSnapshotDto snapshotAvec(int score, boolean officer, String agStatut) {
    return new ComplianceSnapshotDto(
        UUID.randomUUID(), "Test Mutuelle", "SYSCOHADA",
        officer,
        new ConformiteIndicateurDto(agStatut, ""),
        new ConformiteIndicateurDto("OK", ""),
        3, new BigDecimal("80"), new BigDecimal("70"),
        new ConformiteIndicateurDto("OPTIONNEL", ""),
        new ConformiteIndicateurDto("EN_VEILLE", ""),
        new BigDecimal("60"),
        score);
  }

  @Test
  @DisplayName("couleurScore — score >=80 → success")
  void couleurSuccess() throws Exception {
    setSnapshot(bean, snapshotAvec(85, true, "OK"));
    assertEquals("success", bean.getCouleurScore());
  }

  @Test
  @DisplayName("couleurScore — 60..79 → warning")
  void couleurWarning() throws Exception {
    setSnapshot(bean, snapshotAvec(70, true, "OK"));
    assertEquals("warning", bean.getCouleurScore());
  }

  @Test
  @DisplayName("couleurScore — <60 → danger")
  void couleurDanger() throws Exception {
    setSnapshot(bean, snapshotAvec(45, true, "OK"));
    assertEquals("danger", bean.getCouleurScore());
  }

  @Test
  @DisplayName("couleurScore — snapshot null → secondary")
  void couleurSecondaire() {
    assertEquals("secondary", bean.getCouleurScore());
  }

  @Test
  @DisplayName("hasAlertes — score 85 + officer OK + AG OK → false")
  void pasDAlertes() throws Exception {
    setSnapshot(bean, snapshotAvec(85, true, "OK"));
    assertFalse(bean.hasAlertes());
  }

  @Test
  @DisplayName("hasAlertes — officer absent → true")
  void alerteOfficerAbsent() throws Exception {
    setSnapshot(bean, snapshotAvec(85, false, "OK"));
    assertTrue(bean.hasAlertes());
  }

  @Test
  @DisplayName("hasAlertes — AG en RETARD → true")
  void alerteAgRetard() throws Exception {
    setSnapshot(bean, snapshotAvec(85, true, "RETARD"));
    assertTrue(bean.hasAlertes());
  }

  @Test
  @DisplayName("hasAlertes — score <60 → true")
  void alerteScoreFaible() throws Exception {
    setSnapshot(bean, snapshotAvec(50, true, "OK"));
    assertTrue(bean.hasAlertes());
  }

  @Test
  @DisplayName("hasAlertes — snapshot null → false")
  void hasAlertesNull() {
    assertFalse(bean.hasAlertes());
  }
}
