//
// 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.apache.couchdb.nouveau.health;

import com.codahale.metrics.health.HealthCheck;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import org.apache.couchdb.nouveau.api.BulkUpdateRequest;
import org.apache.couchdb.nouveau.api.DocumentUpdate;
import org.apache.couchdb.nouveau.api.DocumentUpdateRequest;
import org.apache.couchdb.nouveau.api.IndexDefinition;
import org.apache.couchdb.nouveau.api.SearchRequest;
import org.apache.couchdb.nouveau.api.SearchResults;
import org.apache.couchdb.nouveau.resources.IndexResource;

public final class IndexHealthCheck extends HealthCheck {

    private final IndexResource indexResource;

    public IndexHealthCheck(final IndexResource indexResource) {
        this.indexResource = indexResource;
    }

    @Override
    protected Result check() throws Exception {
        final String name = "___test";
        try {
            indexResource.deletePath(name, null);
        } catch (IOException e) {
            // Ignored, index might not exist yet.
        }

        indexResource.createIndex(
                name,
                new IndexDefinition(
                        OptionalInt.of(IndexDefinition.LATEST_LUCENE_VERSION),
                        "standard",
                        Optional.empty(),
                        OptionalLong.empty()));
        try {
            indexResource.update(
                    name,
                    new BulkUpdateRequest(List.of(new DocumentUpdate(
                            "foo", new DocumentUpdateRequest(0, 1, null, Collections.emptyList())))));
            final SearchRequest searchRequest = new SearchRequest.Builder()
                    .setQuery("_id:foo")
                    .setMinUpdateSeq(1)
                    .build();
            final SearchResults searchResults = indexResource.searchIndex(name, searchRequest);
            if (searchResults.totalHits() == 1) {
                return Result.healthy();
            } else {
                return Result.unhealthy(
                        "Wrong number of search results, expected 1, got %d", searchResults.totalHits());
            }
        } finally {
            indexResource.deletePath(name, null);
        }
    }
}
