package com.njcn.rdms.module.project.service.datascope;

import org.junit.jupiter.api.Test;

import java.util.Set;

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

class ObjectDataScopeTest {

    @Test
    void all_contains_anything() {
        ObjectDataScope scope = ObjectDataScope.all();
        assertThat(scope.getState()).isEqualTo(ObjectDataScope.State.ALL);
        assertThat(scope.contains(1L, "any")).isTrue();
        assertThat(scope.contains(null, null)).isTrue();
    }

    @Test
    void empty_contains_nothing() {
        ObjectDataScope scope = ObjectDataScope.empty();
        assertThat(scope.getState()).isEqualTo(ObjectDataScope.State.EMPTY);
        assertThat(scope.contains(1L, "any")).isFalse();
    }

    @Test
    void idList_with_only_ids_matches_ids() {
        ObjectDataScope scope = ObjectDataScope.idList(Set.of(1L, 2L), Set.of());
        assertThat(scope.contains(1L, null)).isTrue();
        assertThat(scope.contains(99L, null)).isFalse();
    }

    @Test
    void idList_with_only_directionCodes_matches_directions() {
        ObjectDataScope scope = ObjectDataScope.idList(Set.of(), Set.of("system"));
        assertThat(scope.contains(99L, "system")).isTrue();
        assertThat(scope.contains(99L, "embedded")).isFalse();
    }

    @Test
    void idList_with_both_or_semantics() {
        ObjectDataScope scope = ObjectDataScope.idList(Set.of(1L), Set.of("system"));
        assertThat(scope.contains(1L,  "embedded")).isTrue();   // id 命中
        assertThat(scope.contains(99L, "system")).isTrue();     // direction 命中
        assertThat(scope.contains(99L, "embedded")).isFalse();  // 都不命中
    }

    @Test
    void idList_with_empty_inputs_returns_empty_state() {
        ObjectDataScope scope = ObjectDataScope.idList(Set.of(), Set.of());
        assertThat(scope.getState()).isEqualTo(ObjectDataScope.State.EMPTY);
    }

    @Test
    void ids_and_directions_are_unmodifiable() {
        ObjectDataScope scope = ObjectDataScope.idList(Set.of(1L), Set.of("system"));
        org.junit.jupiter.api.Assertions.assertThrows(UnsupportedOperationException.class,
                () -> scope.getIds().add(99L));
        org.junit.jupiter.api.Assertions.assertThrows(UnsupportedOperationException.class,
                () -> scope.getDirectionCodes().add("embedded"));
    }
}
