/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.paimon.schema;

import org.apache.paimon.CoreOptions;
import org.apache.paimon.types.DataField;
import org.apache.paimon.types.DataTypes;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.apache.paimon.schema.SchemaValidation.validateTableSchema;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Tests for primary-key vector index option validation. */
class PrimaryKeyVectorIndexValidationTest {

    @Test
    void testValidPluralPrimaryKeyVectorIndexConfiguration() {
        Map<String, String> options = new HashMap<>();
        options.put(CoreOptions.BUCKET.key(), "1");
        options.put(CoreOptions.DELETION_VECTORS_ENABLED.key(), "true");
        options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), " embedding ");
        options.put("fields.embedding.pk-vector.index.type", "ivf-pq");

        assertThatCode(() -> validateTableSchema(schema(options))).doesNotThrowAnyException();
    }

    @Test
    void testRejectsMultiplePrimaryKeyVectorIndexColumnsForFirstRelease() {
        Map<String, String> options = new HashMap<>();
        options.put(CoreOptions.BUCKET.key(), "1");
        options.put(CoreOptions.DELETION_VECTORS_ENABLED.key(), "true");
        options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding,other_embedding");
        options.put("fields.embedding.pk-vector.index.type", "ivf-pq");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("exactly one column")
                .hasMessageContaining("pk-vector.index.columns");
    }

    @Test
    void testRejectsDuplicatePrimaryKeyVectorIndexColumns() {
        Map<String, String> options = new HashMap<>();
        options.put(CoreOptions.BUCKET.key(), "1");
        options.put(CoreOptions.DELETION_VECTORS_ENABLED.key(), "true");
        options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding,embedding");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("pk-vector.index.columns")
                .hasMessageContaining("duplicate");
    }

    @Test
    void testValidPrimaryKeyVectorIndex() {
        assertThatCode(() -> validateTableSchema(schema(enabledOptions())))
                .doesNotThrowAnyException();
    }

    @Test
    void testRequiresFieldScopedIndexType() {
        Map<String, String> options = enabledOptions();
        options.remove("fields.embedding.pk-vector.index.type");
        options.put("pk-vector.index.type", "ivf-pq");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("fields.embedding.pk-vector.index.type");
    }

    @Test
    void testRequiresPrimaryKeyTable() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.BUCKET_KEY.key(), "id");
        assertThatThrownBy(
                        () ->
                                validateTableSchema(
                                        new TableSchema(
                                                0,
                                                fields(),
                                                0,
                                                Collections.emptyList(),
                                                Collections.emptyList(),
                                                options,
                                                "")))
                .hasMessageContaining("requires a primary-key table");
    }

    @Test
    void testRequiresDeletionVectors() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.DELETION_VECTORS_ENABLED.key(), "false");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("requires deletion-vectors.enabled = true");
    }

    @Test
    void testSupportsPartialUpdateMergeEngine() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.MERGE_ENGINE.key(), "partial-update");

        assertThatCode(() -> validateTableSchema(schema(options))).doesNotThrowAnyException();
    }

    @Test
    void testSupportsAggregationMergeEngine() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.MERGE_ENGINE.key(), "aggregation");

        assertThatCode(() -> validateTableSchema(schema(options))).doesNotThrowAnyException();
    }

    @Test
    void testIgnoresMergeOnReadForFirstRowWithoutDeletionVectors() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.MERGE_ENGINE.key(), "first-row");
        options.put(CoreOptions.DELETION_VECTORS_ENABLED.key(), "false");
        options.put(CoreOptions.DELETION_VECTORS_MERGE_ON_READ.key(), "true");

        assertThatCode(() -> validateTableSchema(schema(options))).doesNotThrowAnyException();
    }

    @Test
    void testRejectsFirstRowWithDeletionVectors() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.MERGE_ENGINE.key(), "first-row");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining(
                        "First row merge engine does not need deletion vectors because there is no deletion of old data in this merge engine");
    }

    @Test
    void testPartialUpdateRejectsDeletionVectorMergeOnRead() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.MERGE_ENGINE.key(), "partial-update");
        options.put(CoreOptions.DELETION_VECTORS_MERGE_ON_READ.key(), "true");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining(
                        "partial-update requires deletion-vectors.merge-on-read = false");
    }

    @Test
    void testDeduplicateRejectsDeletionVectorMergeOnRead() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.DELETION_VECTORS_MERGE_ON_READ.key(), "true");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("requires deletion-vectors.merge-on-read = false");
    }

    @Test
    void testSupportsPostponeBucket() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.BUCKET.key(), "-2");

        assertThatCode(() -> validateTableSchema(schema(options))).doesNotThrowAnyException();
    }

    @Test
    void testRejectsDynamicBucket() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.BUCKET.key(), "-1");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("requires fixed or postpone bucket mode");
    }

    @Test
    void testRejectsPkClusteringOverride() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.PK_CLUSTERING_OVERRIDE.key(), "true");
        options.put(CoreOptions.CLUSTERING_COLUMNS.key(), "payload");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("does not support pk-clustering-override");
    }

    @Test
    void testRequiresVectorColumn() {
        Map<String, String> options = enabledOptions();
        options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "payload");
        options.put("fields.payload.pk-vector.index.type", "ivf-pq");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("must reference a VECTOR column");
    }

    @Test
    void testIndexNameIsNotRequired() {
        Map<String, String> options = enabledOptions();
        assertThatCode(() -> validateTableSchema(schema(options))).doesNotThrowAnyException();
    }

    @Test
    void testRequiresFloatVectorElements() {
        TableSchema schema =
                new TableSchema(
                        0,
                        Arrays.asList(
                                new DataField(0, "id", DataTypes.INT().notNull()),
                                new DataField(
                                        1, "embedding", DataTypes.VECTOR(8, DataTypes.DOUBLE()))),
                        1,
                        Collections.emptyList(),
                        Collections.singletonList("id"),
                        enabledOptions(),
                        "");

        assertThatThrownBy(() -> validateTableSchema(schema))
                .hasMessageContaining("must use FLOAT elements");
    }

    @Test
    void testRejectsUnsupportedDistanceMetric() {
        Map<String, String> options = enabledOptions();
        options.put("fields.embedding.pk-vector.distance.metric", "manhattan");

        assertThatThrownBy(() -> validateTableSchema(schema(options)))
                .hasMessageContaining("pk-vector.distance.metric")
                .hasMessageContaining("l2, cosine, inner_product");
    }

    private static Map<String, String> enabledOptions() {
        Map<String, String> options = new HashMap<>();
        options.put(CoreOptions.BUCKET.key(), "1");
        options.put(CoreOptions.DELETION_VECTORS_ENABLED.key(), "true");
        options.put(CoreOptions.PK_VECTOR_INDEX_COLUMNS.key(), "embedding");
        options.put("fields.embedding.pk-vector.index.type", "ivf-pq");
        return options;
    }

    private static TableSchema schema(Map<String, String> options) {
        return new TableSchema(
                0,
                fields(),
                0,
                Collections.emptyList(),
                Collections.singletonList("id"),
                options,
                "");
    }

    private static java.util.List<DataField> fields() {
        return Arrays.asList(
                new DataField(0, "id", DataTypes.INT().notNull()),
                new DataField(1, "embedding", DataTypes.VECTOR(8, DataTypes.FLOAT())),
                new DataField(2, "payload", DataTypes.STRING()));
    }
}
