/*
 * Copyright 2021 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.kie.kogito.index.test;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.function.BiConsumer;

import org.junit.jupiter.api.AfterEach;
import org.kie.kogito.persistence.api.Storage;
import org.kie.kogito.persistence.api.query.AttributeFilter;
import org.kie.kogito.persistence.api.query.AttributeSort;

public abstract class QueryTestBase<K, V> {

    public abstract Storage<K, V> getStorage();

    protected Boolean isDateTimeAsLong() {
        return true;
    }

    @AfterEach
    void tearDown() {
        getStorage().clear();
    }

    public void queryAndAssert(BiConsumer<List<V>, String[]> assertConsumer, Storage<K, V> storage, List<AttributeFilter<?>> filters, List<AttributeSort> sort, Integer offset, Integer limit,
            String... ids) {
        assertConsumer.accept(storage.query().filter(filters).sort(sort).offset(offset).limit(limit).execute(), ids);
    }

    protected Object getDateTime(ZonedDateTime dateTime) {
        return isDateTimeAsLong() ? dateTime.toInstant().toEpochMilli() : dateTime;
    }

    protected Object getDateTime() {
        return isDateTimeAsLong() ? Instant.now().plus(1, ChronoUnit.DAYS).toEpochMilli() : ZonedDateTime.now().plus(1, ChronoUnit.DAYS);
    }
}
