@page "/admin/commands"
@page "/admin/commands/{DatabaseStr}/{CollectionStr}"
@page "/admin/commands/{DatabaseStr}/{DatabaseInstanceStr}/{CollectionStr}"
@attribute [Authorize]

@using System.Reflection
@using Rms.Risk.Mango.Components.Commands

@inject NavigationManager           NavigationManager
@inject IUserSession                UserSession
@inject IJSRuntime                  JsRuntime
@inject AuthenticationStateProvider AuthProvider;
@inject IAuthorizationService       AuthService;

@*
 *                                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>
    .js-field {
        height: 300px;
        position: relative;
    }

    .res-field {
        width: 100vw;
        height: calc(100vh - (150px + 300px));
        overflow: auto;
    }

    pre {
        width: 100%;
        height: 100%;
        font-size: 10pt;
        background-color: #1d2a3b;
        color: #FFFFFF;
    }

    .nav-tabs {
        border-bottom: 0 none #415369;
        border-right: 1px solid #415369;
        margin-right: 12px;
    }

    .nav-tabs .nav-link.active {
        color: white;
        background-color: #415369;
    }

    .CodeMirror {
        width: 600px !important;
        height: 300px !important;
        max-height: 300px !important;
    }

    .tab-group {
        color: yellow;
    }
    .res-json {
        max-width: 80vw;
        max-height: 80vh;
        overflow: auto;
        margin-left: 10px;
        margin-right: 10px;
    }

    .cmd-json {
        min-width: 410px;
    }
</style>

<h3 class="mt-3">Commands</h3>

<AuthorizedOnly Policy="ReadAccess" Resource="@UserSession.Database">
    <EditForm EditContext="EditContext">

        <div class="form-row">
            <FormItemSelect Enabled="@IsReady" Class="mr-2" Name="Collection" @bind-Value="SelectedCollection" Values="@Collections" Icon="icon-folder-outline-sm"/>
            <FormItemSelect Enabled="@IsReady" Class="mr-2" Name="Shard (if needed)" @bind-Value="Shard" Values="Shards" />
            <FormItemText Enabled="@IsReady" Class="mr-2" Name="Timeout (sec)" @bind-Value="Timeout" InputType="number" Icon="icon-clock-sm" />
            <div class="form-group">
                <label>&nbsp;</label>
                <FormButton Enabled="@IsReadyToRun" Name="Run" Icon="icon-running-man-sm" OnClick="Execute" IsPrimary="true" />
            </div>
        </div>

    </EditForm>

    <TabControl Class="row flex-stack-horizontal px-3" Vertical="true" @bind-ActivePage="SelectedCommand" TabGroupClass="tab-group" PersistAllTabs="true">
        @foreach (var (group, controls) in _userAvailableCommands)
        {
            <TabPage Text="@group" IsSelectable="false"/>
            @foreach (var (name, _, control) in controls)
            {
                var parameters = new Dictionary<string, object>()
                {
                    ["Collection"] = SelectedCollection,
                    ["Parameters"] = _commandParams[name]
                };

                <TabPage Text="@name">
                    <DynamicComponent Type="@control" Parameters="@parameters"/>
                </TabPage>
            }
        }
    </TabControl>

    @if (!string.IsNullOrWhiteSpace(Error))
    {
        <pre class="wf-converter-syntax-error">@Error</pre>
    }
</AuthorizedOnly>

@code {
    [CascadingParameter] public IModalService Modal { get; set; } = null!;

    [Parameter] public string? DatabaseStr   { get; set; }
    [Parameter] public string? DatabaseInstanceStr { get; set; }
    [Parameter] public string? CollectionStr { get; set; }

    private IReadOnlyCollection<string> Collections { get; set; } = [];

    private string SelectedCollection
    {
        get => UserSession.Collection;
        set
        {
            if (UserSession.Collection == value)
                return;
            UserSession.Collection = value;
            SyncUrl();
            Error = "";
            InvokeAsync(StateHasChanged);
        }
    }

    private string Database
    {
        get => UserSession.Database;
        set
        {
            if (UserSession.Database == value)
                return;
            UserSession.Database = value;
            SyncUrl();
        }
    }

    private string DatabaseInstance
    {
        get => UserSession.DatabaseInstance;
        set
        {
            if (UserSession.DatabaseInstance == value)
                return;
            UserSession.DatabaseInstance = value;
            SyncUrl();
        }
    }

    private string SelectedCommand
    {
        get;
        set
        {
            if (field == value || value == null || !_commandParams.ContainsKey(value))
                return;
            field = value;
            Error = "";
            InvokeAsync(StateHasChanged);
        }
    } = "Statistics";

    private static readonly string[] _groupsOrder =
    [
        "Informational",
        "Admin",
        "Collections",
        "Indexes",
        "Documents"
    ];


    private string Timeout { get; set; } = "20";
    private string Error   { get; set; } = "";
    private bool   IsReady { get; set; }

    private bool IsReadyToRun => IsReady && CanExecute && !string.IsNullOrWhiteSpace(CommandJson);

    private EditContext? _editContext;
    private EditContext  EditContext => _editContext!;

    private string                                       CommandName            => _commandParams[SelectedCommand].Name;
    private CmdType                                      CommandType            => _commandParams[SelectedCommand].CommandType;
    private string                                       CommandJson            => _commandParams[SelectedCommand].CommandJson;
    private bool                                         CanExecute             => _commandParams[SelectedCommand].CanExecute;
    private bool                                         NeedConfirmation       => _commandParams[SelectedCommand].NeedConfirmation;
    private CmdBase.CommandParams.DatabaseConnectionType RequiredConnectionType => _commandParams[SelectedCommand].RequiredConnectionType;

    private static readonly List<(string Group, List<(string Name, CmdType CommandType, Type Control)> Controls)> _availableCommands;
    private                 List<(string Group, List<(string Name, CmdType CommandType, Type Control)> Controls)> _userAvailableCommands = [];

    private                 Dictionary<string, CmdBase.CommandParams> _commandParams          = null!;
    private readonly        Dictionary<string, Shell.ShardProperties> _shardConnectionStrings = [];

    private bool _isWrite;
    private bool _isRead;
    private bool _isAdmin;

    private string                      Shard  { get; set; } = "";
    private IReadOnlyCollection<string> Shards => _shardConnectionStrings.Keys.OrderBy(x => x).ToList();

    private List<BsonDocument> Result
    {
        get => _commandParams[SelectedCommand].Result;
        set => _commandParams[SelectedCommand].Result = value;
    }

    static AdminCommands()
    {
        var types = typeof(AdminCommands).Assembly.GetTypes()
           .Select(x =>
            {
                var attr = x.GetCustomAttribute(typeof(MongoCommandAttribute)) as MongoCommandAttribute;
                return (Type: x, Attr: attr);
            })
           .Where(x => x.Attr != null)
           .GroupBy(x => x.Attr!.Group)
           .Select(x => (Group: x.Key, Commands: x.OrderBy(y => y.Attr!.Name).Select(y => (y.Attr!.Name, CommandType: y.Attr.Type, Control: y.Type) ).ToList()))
           .Where(x => _groupsOrder.Contains(x.Group))
           .OrderBy(x => Array.IndexOf(_groupsOrder, x.Group))
           .ToList()
        ;

        _availableCommands = types;
    }

    protected override void OnInitialized()
    {
        _editContext  = new(this);

        _commandParams = new(
            _availableCommands
               .SelectMany(x => x.Controls)
               .Select(x => new KeyValuePair<string, CmdBase.CommandParams>(x.Name, new (x.Name, x.CommandType, OnChanged))
                )
            );
    }

    private void OnChanged(CmdBase.CommandParams arg)
    {
        InvokeAsync(StateHasChanged);
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender)
            return;

        if (string.IsNullOrWhiteSpace(DatabaseStr))
            DatabaseStr = Database;
        else
            Database = DatabaseStr;

        if (string.IsNullOrWhiteSpace(DatabaseInstanceStr))
            DatabaseInstanceStr = DatabaseInstance;
        else
            DatabaseInstance = DatabaseInstanceStr;


        if (string.IsNullOrWhiteSpace(CollectionStr))
            CollectionStr = SelectedCollection;
        else
            SelectedCollection = CollectionStr;

        SyncUrl();
        await Authorize();
        SelectAvailableCommands();

        _ = Task.Run(async () =>
        {
            try
            {
                await Task.WhenAll( LoadCollections(), LoadShards() );

                IsReady = true;
                await InvokeAsync(StateHasChanged);
            }
            catch (Exception e)
            {
                await Display(e);
            }
        });
        SyncUrl();
        StateHasChanged();
    }

    private void SelectAvailableCommands()
    {
        _userAvailableCommands = _availableCommands
            .Select(x =>
                x with {
                    Controls = x.Controls
                        .Where(y =>
                            y.CommandType switch 
                            { 
                                CmdType.Admin => _isAdmin, 
                                CmdType.Read  => _isRead, 
                                CmdType.Write => _isWrite, 
                                _             => false 
                            })
                        .ToList() 
                })
           .Where(x => x.Controls.Count > 0)
           .ToList();
    }

    private async Task Authorize()
    {
        var user = (await AuthProvider.GetAuthenticationStateAsync()).User;

        var readTask  = AuthService.AuthorizeAsync(user, UserSession.Database, "ReadAccess");
        var writeTask = AuthService.AuthorizeAsync(user, UserSession.Database, "WriteAccess");
        var adminTask = AuthService.AuthorizeAsync(user, UserSession.Database, "AdminAccess");

        var results = await Task.WhenAll(readTask, writeTask, adminTask);

        _isRead  = results[0].Succeeded;
        _isWrite = results[1].Succeeded;
        _isAdmin = results[2].Succeeded;
    }

    private async Task LoadCollections()
    {
        var admin = UserSession.MongoDbAdmin;

        Collections = await admin.ListCollections();
        if (!Collections.Contains(SelectedCollection))
            SelectedCollection = Collections.FirstOrDefault() ?? "";
    }

    private async Task LoadShards()
    {
        var shards = await Shell.LoadShards(UserSession);
        _shardConnectionStrings.Clear();
        foreach (var shard in shards)
        {
            _shardConnectionStrings.Add(shard.Key, shard.Value);
        }
    }

    private Task Display(Exception e)
    {
        Error = e.ToString();
        return InvokeAsync(StateHasChanged);
    }

    private Task Display(List<BsonDocument> res)
    {
        Result = res;
        return InvokeAsync(StateHasChanged);
    }

    private async Task<List<BsonDocument>> RunCommand(BsonDocument doc, CancellationToken token)
    {
        var service = RequiredConnectionType switch
        {
            CmdBase.CommandParams.DatabaseConnectionType.Admin   => UserSession.MongoDbAdminForAdminDatabase,
            CmdBase.CommandParams.DatabaseConnectionType.Cluster => UserSession.MongoDbAdmin,
            CmdBase.CommandParams.DatabaseConnectionType.Shard   => _shardConnectionStrings.Count == 0 
                ? UserSession.MongoDbAdmin 
                : UserSession.GetShardConnection(_shardConnectionStrings[Shard ?? throw new("Shard is not selected")].Host, _shardConnectionStrings[Shard].Port),
            _                                                    => throw new($"Unknown connection type {RequiredConnectionType}")
        };

        var result = await service.RunCommand(doc, token);
        return [result];
    }
    private void SyncUrl()
    {
        var url = NavigationManager.BaseUri + $"admin/commands/{Database}";
        if ( !string.IsNullOrWhiteSpace(DatabaseInstance) )
            url += $"/{DatabaseInstance}";
        url += $"/{SelectedCollection}";

        JsRuntime.InvokeAsync<string>("DashboardUtils.ChangeUrl", url);
    }

    private Task<string?> CanExecuteCommand()
        => Shell.CanExecuteCommand(UserSession, InvokeAsync, Modal);


    private async Task Execute()
    {
        Error = "";
        if (string.IsNullOrWhiteSpace(CommandJson))
            return;

        if (NeedConfirmation)
        {
            var res = await ModalDialogUtils.ShowConfirmationDialog(Modal, "Confirmation", $"Are you sure executing command \"{CommandName}\"? Some command's effects are irreversible!");
            if (!res.Confirmed)
                return;
        }

        var ticket =  CommandType == CmdType.Read ? "N/A" : await CanExecuteCommand();
        if (string.IsNullOrWhiteSpace(ticket))
            return;

        var cts = new CancellationTokenSource(TimeSpan.FromSeconds(int.Parse(Timeout)));

        try
        {
            IsReady = false;
            await InvokeAsync(StateHasChanged);

            var doc = BsonDocument.Parse(CommandJson);
            Shell.UpdateComment(doc, ticket, UserSession.User.GetEmail());

            var res = await RunCommand(doc, cts.Token);
            await Display(res);
        }
        catch (Exception e)
        {
            await Display(e);
        }
        finally
        {
            IsReady = true;
            await InvokeAsync(StateHasChanged);
        }

    }


}
