@inherits CmdBase
@attribute [MongoCommand(CmdType.Read, "Statistics", "Informational")]

@*
 *                                dbMango
 *
 * Copyright 2025 Deutsche Bank AG
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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.
 *@

<style>
    .stats-table {
        width: auto;
        height: 800px !important;
    }
    .CodeMirror {
        width: 100%;
        max-width: 800px;
        height: 800px !important;
    }
</style>

<div class="@Class">
    <div class="form-row">
        <FormJson Value="@(Parameters?.CommandJson)" Class="cmd-json" />
    </div>

    <div class="row flex-stack-horizontal">
        <div class="stats-table mr-3 ml-3">
            <TableControl Class="table table-hover table-striped table-forge table-forge-striped mr-3" Items="@Stats" PageSize="20" Filterable="false">
                <TableColumnControl Name="Name" Field="Key" Class="column-name"/>
                <TableColumnControl Name="Value" Field="Value" Class="column-name"/>
            </TableControl>
        </div>
        <FormJson Value="@Result" Class="res-json"/>
    </div>
</div>


@code {
    //

    private string        Json  { get; set; } = "";
    private List<dynamic> Stats { get; set; } = [];

    private static readonly List<(string, string, bool)> _descriptor =
    [
        ("ns"             , "Full name"           , false),
        ("sharded"        , "Is sharded"          , false),
        ("count"          , "Number of documents" , false),
        ("size"           , "Size"                , false),
        ("storageSize"    , "Storage size"        , true ),
        ("totalIndexSize" , "Total index size"    , true ),
        ("totalSize"      , "Total size"          , true ),
        ("avgObjSize"     , "Average object size" , true ),
        ("maxSize"        , "Max size"            , true ),
        ("nindexes"       , "Number of indexes"   , false),
        ("nchunks"        , "Number of chunks"    , false)
    ];

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender )
            UpdateCommand();
        return Task.CompletedTask;
    }

    private void UpdateCommand()
    {
        if (Parameters == null)
            return;
        Parameters.CommandJson = $@"{{
    ""collStats"": ""{Collection}"",
    ""scale"": 1
}}";
        Parameters.NeedConfirmation = false;
        Validate();
    }

    protected override Task OnCollectionChanged()
    {
        UpdateCommand();
        return Task.CompletedTask;
    }

    protected override void OnResultReceived()
    {
        if (Parameters?.Result.Count > 0)
        {
            Json = Parameters.Result[0].ToJson(new() { Indent = true });
            Stats = FormatDocument(Parameters.Result[0], _descriptor).Cast<dynamic>().ToList();
        }
        else
        {
            Json  = "";
            Stats = [];
        }

        InvokeAsync(StateHasChanged);
    }

}
