package org.raddatz.familienarchiv.user;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.UUID;

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

class AppUserTest {

    private static final List<String> EXPECTED_PALETTE = List.of(
        "#7a4f9a", "#5a8a6a", "#3060b0", "#a0522d", "#c0446e", "#c17a00", "#0e7490", "#1d4ed8"
    );

    @Test
    void computeColor_returnsDeterministicPaletteColor() {
        UUID id = UUID.fromString("12345678-1234-1234-1234-123456789abc");
        String color = AppUser.computeColor(id);
        assertThat(EXPECTED_PALETTE).contains(color);
        assertThat(AppUser.computeColor(id)).isEqualTo(color);
    }

    @Test
    void computeColor_isStableAcrossCalls() {
        UUID id = UUID.randomUUID();
        assertThat(AppUser.computeColor(id)).isEqualTo(AppUser.computeColor(id));
    }

    @Test
    void computeColor_variesAcrossDifferentIds() {
        long distinct = java.util.stream.IntStream.range(0, 100)
            .mapToObj(i -> AppUser.computeColor(UUID.randomUUID()))
            .distinct()
            .count();
        assertThat(distinct).isGreaterThan(1);
    }

    @Test
    void computeColor_returnsValidPaletteColorForIntegerMinValueHash() {
        // UUID "80000000-0000-0000-0000-000000000000" has hashCode() == Integer.MIN_VALUE.
        // Math.abs(Integer.MIN_VALUE) overflows back to Integer.MIN_VALUE (negative), making
        // Math.abs(hashCode()) % n unsafe for palette sizes that don't evenly divide MIN_VALUE.
        // Math.floorMod eliminates this edge case entirely.
        UUID minHashId = UUID.fromString("80000000-0000-0000-0000-000000000000");
        assertThat(minHashId.hashCode()).isEqualTo(Integer.MIN_VALUE);
        assertThat(EXPECTED_PALETTE).contains(AppUser.computeColor(minHashId));
    }
}
