package com.cameleer.server.core.search;

import org.junit.jupiter.api.Test;

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

class AttributeFilterTest {

    @Test
    void keyOnly_blankValue_normalizesToNull() {
        AttributeFilter f = new AttributeFilter("order", "");
        assertThat(f.value()).isNull();
        assertThat(f.isKeyOnly()).isTrue();
        assertThat(f.isWildcard()).isFalse();
    }

    @Test
    void keyOnly_nullValue_isKeyOnly() {
        AttributeFilter f = new AttributeFilter("order", null);
        assertThat(f.isKeyOnly()).isTrue();
    }

    @Test
    void exactValue_isNotWildcard() {
        AttributeFilter f = new AttributeFilter("order", "47");
        assertThat(f.isKeyOnly()).isFalse();
        assertThat(f.isWildcard()).isFalse();
    }

    @Test
    void starInValue_isWildcard() {
        AttributeFilter f = new AttributeFilter("order", "47*");
        assertThat(f.isWildcard()).isTrue();
    }

    @Test
    void invalidKey_throws() {
        assertThatThrownBy(() -> new AttributeFilter("bad key", "x"))
                .isInstanceOf(IllegalArgumentException.class)
                .hasMessageContaining("attribute key");
    }

    @Test
    void blankKey_throws() {
        assertThatThrownBy(() -> new AttributeFilter(" ", null))
                .isInstanceOf(IllegalArgumentException.class);
    }

    @Test
    void wildcardPattern_escapesLikeMetaCharacters() {
        AttributeFilter f = new AttributeFilter("order", "a_b%c\\d*");
        assertThat(f.toLikePattern()).isEqualTo("a\\_b\\%c\\\\d%");
    }

    @Test
    void exactValue_toLikePattern_returnsNull() {
        AttributeFilter f = new AttributeFilter("order", "47");
        assertThat(f.toLikePattern()).isNull();
    }

    @Test
    void searchRequest_canonicalCtor_acceptsAttributeFilters() {
        SearchRequest r = new SearchRequest(
                null, null, null, null, null, null, null, null, null, null,
                null, null, null, null, null, 0, 50, null, null, null, null,
                java.util.List.of(new AttributeFilter("order", "47")));
        assertThat(r.attributeFilters()).hasSize(1);
        assertThat(r.attributeFilters().get(0).key()).isEqualTo("order");
    }

    @Test
    void searchRequest_legacyCtor_defaultsAttributeFiltersToEmpty() {
        SearchRequest r = new SearchRequest(
                null, null, null, null, null, null, null, null, null, null,
                null, null, null, null, null, 0, 50, null, null, null, null);
        assertThat(r.attributeFilters()).isEmpty();
    }

    @Test
    void searchRequest_compactCtor_normalizesNullAttributeFilters() {
        SearchRequest r = new SearchRequest(
                null, null, null, null, null, null, null, null, null, null,
                null, null, null, null, null, 0, 50, null, null, null, null,
                null);
        assertThat(r.attributeFilters()).isNotNull().isEmpty();
    }
}
