@page "/AdminSettings/CouchLogSettings"
@rendermode InteractiveServer

@using System.Reflection
@using CouchLog.Data
@using Microsoft.EntityFrameworkCore

@inject ApplicationDbContext CouchLogDb

<MudText Typo="Typo.h3">CouchLog Settings</MudText>

<EditForm Model="_accountsSettings">
    <MudSwitch T="bool"
               Label="Is Registration allowed"
               Value="_accountsSettings.IsRegistrationAllowed"
               ValueChanged="OnRegistrationChanged"
               Color="Color.Primary" />
</EditForm>

<MudText>Version: @_version</MudText>

@code {
    private AccountsSettings? _accountsSettings;
    private readonly string _version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion?.Split('+')[0];

    protected override async Task OnInitializedAsync()
    {
        _accountsSettings = await CouchLogDb.AccountsSettings.FirstAsync();
    }

    private async Task OnRegistrationChanged(bool value)
    {
        _accountsSettings!.IsRegistrationAllowed = value;

        CouchLogDb.AccountsSettings.Update(_accountsSettings);
        await CouchLogDb.SaveChangesAsync();
    }
}
