using Insight.Infrastructure.Entities;
using Insight.Web.Constants;
using Microsoft.AspNetCore.Components;
using MongoDB.Driver;
using MudBlazor;

namespace Insight.Web.Pages.Management.Accounts;

public partial class AccountEditDialog
{
    [CascadingParameter(Name = "Account")] public InsightUser? Account { get; set; }
    [Parameter] public EventCallback OnChanges { get; set; }

    [Inject] private IMongoDatabase Database { get; init; } = default!;
    [Inject] private ISnackbar Snackbar { get; init; } = default!;
    [Inject] private ILogger<AccountEditDialog> Logger { get; init; } = default!;

    private async Task SubmitAsync()
    {
        if (Account is null) return;

        try
        {
            //await Database.Users()
            //    .UpdateOneAsync(Builders<InsightUser>
            //    .Filter
            //    .Eq(p => p.Id, Account.Id), Builders<InsightUser>
            //    .Update
            //    .Set(p => p.Updated, DateTime.Now)
            //    .Set(p => p.Name, Host.Name)
            //    .Set(p => p.Description, Host.Description),
            //    cancellationToken: default);

            Notification.Success(Snackbar);

            if (OnChanges.HasDelegate)
            {
                await InvokeAsync(async () =>
                {
                    await OnChanges.InvokeAsync(this);
                });
            }
        }
        catch (Exception ex)
        {
            Notification.Error(Snackbar);
            Logger.LogError(ex.ToString());
        }
        finally
        {
            _visible = false;
        }
    }
}