/*
 * Copyright 2025-present the original author or authors.
 *
 * 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
 *
 *      https://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.springframework.data.elasticsearch.repository.support;

import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.elasticsearch.core.IndexOperationsAdapter.*;

import reactor.core.publisher.Flux;

import java.util.List;

import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.SearchTemplateQuery;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.script.Script;
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
import org.springframework.data.elasticsearch.utils.IndexNameProvider;

/**
 * @since 5.5
 */
@SpringIntegrationTest
abstract class ReactiveRepositoryQueryIntegrationTests {
	@Autowired private SampleElasticsearchRepository repository;
	@Autowired private ReactiveElasticsearchOperations operations;
	@Autowired private IndexNameProvider indexNameProvider;

	@BeforeEach
	void before() {
		indexNameProvider.increment();
		blocking(operations.indexOps(LOTRCharacter.class)).createWithMapping();
	}

	@Test
	@org.junit.jupiter.api.Order(Integer.MAX_VALUE)
	public void cleanup() {
		blocking(operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*"))).delete();
	}

	@Test // #2997
	@DisplayName("should use searchtemplate query")
	void shouldUseSearchtemplateQuery() {
		// store some data
		repository.saveAll(List.of(
				new LOTRCharacter("1", "Frodo is a hobbit"),
				new LOTRCharacter("2", "Legolas is an elf"),
				new LOTRCharacter("3", "Gandalf  is a wizard"),
				new LOTRCharacter("4", "Bilbo is a hobbit"),
				new LOTRCharacter("5", "Gimli is a dwarf")))
				.blockLast();

		// store a searchtemplate
		String searchInCharacter = """
				{
					"query": {
						"bool": {
							"must": [
								{
									"match": {
										"lotrCharacter": "{{word}}"
									}
								}
							]
						}
					},
					"from": 0,
					"size": 100,
					"sort": {
						"id": {
							"order": "desc"
						}
					}
				  }
				""";

		Script scriptSearchInCharacter = Script.builder() //
				.withId("searchInCharacter") //
				.withLanguage("mustache") //
				.withSource(searchInCharacter) //
				.build();

		var success = operations.putScript(scriptSearchInCharacter).block();
		assertThat(success).isTrue();

		// search with repository for hobbits order by id descending
		var searchHits = repository.searchInCharacter("hobbit")
				.collectList().block();

		// check result (bilbo, frodo)
		assertThat(searchHits).isNotNull();
		assertThat(searchHits.size()).isEqualTo(2);
		assertThat(searchHits.get(0).getId()).isEqualTo("4");
		assertThat(searchHits.get(1).getId()).isEqualTo("1");
	}

	@Document(indexName = "#{@indexNameProvider.indexName()}")
	static class LOTRCharacter {
		@Nullable
		@Id
		@Field(fielddata = true) // needed for the sort to work
		private String id;

		@Field(type = FieldType.Text)
		@Nullable private String lotrCharacter;

		public LOTRCharacter(@Nullable String id, @Nullable String lotrCharacter) {
			this.id = id;
			this.lotrCharacter = lotrCharacter;
		}

		@Nullable
		public String getId() {
			return id;
		}

		public void setId(@Nullable String id) {
			this.id = id;
		}

		@Nullable
		public String getLotrCharacter() {
			return lotrCharacter;
		}

		public void setLotrCharacter(@Nullable String lotrCharacter) {
			this.lotrCharacter = lotrCharacter;
		}
	}

	interface SampleElasticsearchRepository
			extends ReactiveElasticsearchRepository<LOTRCharacter, String> {
		@SearchTemplateQuery(id = "searchInCharacter")
		Flux<SearchHit<LOTRCharacter>> searchInCharacter(String word);
	}
}
