// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package com.azure.search.documents.indexes.models;

import com.azure.core.annotation.Immutable;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Statistics for a given index. Statistics are collected periodically and are not guaranteed to always be up-to-date.
 */
@Immutable
public final class SearchIndexStatistics implements JsonSerializable<SearchIndexStatistics> {
    /*
     * The number of documents in the index.
     */
    private final long documentCount;

    /*
     * The amount of storage in bytes consumed by the index.
     */
    private final long storageSize;

    /*
     * The amount of memory in bytes consumed by vectors in the index.
     */
    private Long vectorIndexSize;

    /**
     * Creates an instance of SearchIndexStatistics class.
     *
     * @param documentCount the documentCount value to set.
     * @param storageSize the storageSize value to set.
     */
    public SearchIndexStatistics(long documentCount, long storageSize) {
        this.documentCount = documentCount;
        this.storageSize = storageSize;
    }

    /**
     * Get the documentCount property: The number of documents in the index.
     *
     * @return the documentCount value.
     */
    public long getDocumentCount() {
        return this.documentCount;
    }

    /**
     * Get the storageSize property: The amount of storage in bytes consumed by the index.
     *
     * @return the storageSize value.
     */
    public long getStorageSize() {
        return this.storageSize;
    }

    /**
     * Get the vectorIndexSize property: The amount of memory in bytes consumed by vectors in the index.
     *
     * @return the vectorIndexSize value.
     */
    public Long getVectorIndexSize() {
        return this.vectorIndexSize;
    }

    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeLongField("documentCount", this.documentCount);
        jsonWriter.writeLongField("storageSize", this.storageSize);
        jsonWriter.writeNumberField("vectorIndexSize", this.vectorIndexSize);
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of SearchIndexStatistics from the JsonReader.
     *
     * @param jsonReader The JsonReader being read.
     * @return An instance of SearchIndexStatistics if the JsonReader was pointing to an instance of it, or null if it
     *     was pointing to JSON null.
     * @throws IllegalStateException If the deserialized JSON object was missing any required properties.
     * @throws IOException If an error occurs while reading the SearchIndexStatistics.
     */
    public static SearchIndexStatistics fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(
                reader -> {
                    boolean documentCountFound = false;
                    long documentCount = 0L;
                    boolean storageSizeFound = false;
                    long storageSize = 0L;
                    Long vectorIndexSize = null;
                    while (reader.nextToken() != JsonToken.END_OBJECT) {
                        String fieldName = reader.getFieldName();
                        reader.nextToken();

                        if ("documentCount".equals(fieldName)) {
                            documentCount = reader.getLong();
                            documentCountFound = true;
                        } else if ("storageSize".equals(fieldName)) {
                            storageSize = reader.getLong();
                            storageSizeFound = true;
                        } else if ("vectorIndexSize".equals(fieldName)) {
                            vectorIndexSize = reader.getNullable(JsonReader::getLong);
                        } else {
                            reader.skipChildren();
                        }
                    }
                    if (documentCountFound && storageSizeFound) {
                        SearchIndexStatistics deserializedSearchIndexStatistics =
                                new SearchIndexStatistics(documentCount, storageSize);
                        deserializedSearchIndexStatistics.vectorIndexSize = vectorIndexSize;

                        return deserializedSearchIndexStatistics;
                    }
                    List<String> missingProperties = new ArrayList<>();
                    if (!documentCountFound) {
                        missingProperties.add("documentCount");
                    }
                    if (!storageSizeFound) {
                        missingProperties.add("storageSize");
                    }

                    throw new IllegalStateException(
                            "Missing required property/properties: " + String.join(", ", missingProperties));
                });
    }
}
