@page "/settings"
@attribute [Authorize]

@using Microsoft.AspNetCore.Components.Forms
@using WorkTracker.Services.Storage

@inject IAppSettingsService AppSettingsService
@inject IDatabaseBackupService DatabaseBackupService
@inject AppThemeState ThemeState
@inject IJSRuntime JS

<PageTitle>Settings</PageTitle>

<h1>Settings</h1>
<p class="text-muted">Default values used to compute manual work-unit totals and income.</p>

@if (settings is null)
{
    <p><em>Loading...</em></p>
}
else
{
    <EditForm Model="settings" OnValidSubmit="SaveAsync">
        <DataAnnotationsValidator />

        <div class="row g-3">
            <div class="col-12 col-md-6">
                <label class="form-label">Theme</label>
                <InputSelect class="form-select" @bind-Value="settings.ThemeMode">
                    <option value="@AppThemeMode.System">Follow system</option>
                    <option value="@AppThemeMode.Light">Light</option>
                    <option value="@AppThemeMode.Dark">Dark</option>
                </InputSelect>
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label">Standard work hours/day</label>
                <InputNumber class="form-control" @bind-Value="settings.StandardWorkHoursPerDay" />
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label">Hourly gross rate (€)</label>
                <InputNumber class="form-control" @bind-Value="settings.HourlyGrossRate" />
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label">Profitability coefficient</label>
                <InputNumber class="form-control" @bind-Value="settings.ProfitabilityCoefficient" />
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label">INPS rate</label>
                <InputNumber class="form-control" @bind-Value="settings.InpsRate" />
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label">Substitute tax rate</label>
                <InputNumber class="form-control" @bind-Value="settings.SubstituteTaxRate" />
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label">Locale</label>
                <InputText class="form-control" @bind-Value="settings.Locale" />
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label">Currency</label>
                <InputText class="form-control" @bind-Value="settings.Currency" />
            </div>
        </div>

        <div class="d-flex align-items-center gap-2 mt-4">
            <button class="btn btn-primary" type="submit">Save</button>
            @if (!string.IsNullOrWhiteSpace(statusMessage))
            {
                <span class="text-success">@statusMessage</span>
            }
        </div>
    </EditForm>

    <section class="mt-5">
        <h2 class="h4">Database backup</h2>
        <p class="text-muted mb-3">Export the full database as JSON or restore a previously exported JSON backup. Restore replaces the current database only when the backup format version and database schema version are supported.</p>

        <div class="d-flex flex-wrap gap-2 align-items-center mb-4">
            <a class="btn btn-outline-secondary" href="/api/database-backup/export">Export JSON backup</a>
            <span class="small text-muted">Current database schema version: @DatabaseSchemaVersion</span>
        </div>

        <div class="card border-danger-subtle">
            <div class="card-body">
                <h3 class="h6 card-title">Restore from JSON</h3>
                <p class="card-text text-muted">This overwrites the existing database with the selected backup file.</p>

                <div class="d-flex flex-column gap-3 align-items-start">
                    <InputFile OnChange="OnRestoreFileSelected" accept="application/json,.json" />

                    @if (!string.IsNullOrWhiteSpace(selectedBackupFileName))
                    {
                        <div class="small text-muted">Selected file: @selectedBackupFileName</div>
                    }

                    <button class="btn btn-outline-danger" type="button" @onclick="RestoreAsync" disabled="@(selectedBackupFile is null || isRestoring)">
                        @(isRestoring ? "Restoring..." : "Restore JSON backup")
                    </button>
                </div>

                @if (!string.IsNullOrWhiteSpace(backupStatusMessage))
                {
                    <div class="alert alert-success py-2 mt-3 mb-0">@backupStatusMessage</div>
                }

                @if (!string.IsNullOrWhiteSpace(backupErrorMessage))
                {
                    <div class="alert alert-danger py-2 mt-3 mb-0">@backupErrorMessage</div>
                }
            </div>
        </div>
    </section>
}

@code {
    private const long MaxBackupFileSize = 20 * 1024 * 1024;

    private AppSettingsDocument? settings;
    private string? statusMessage;
    private string? backupStatusMessage;
    private string? backupErrorMessage;
    private IBrowserFile? selectedBackupFile;
    private string? selectedBackupFileName;
    private bool isRestoring;

    private int DatabaseSchemaVersion => CouchbaseLiteDatabaseProvider.CurrentDatabaseSchemaVersion;

    protected override async Task OnInitializedAsync()
    {
        settings = await AppSettingsService.GetAsync();
    }

    private async Task SaveAsync()
    {
        if (settings is null)
        {
            return;
        }

        settings = await AppSettingsService.SaveAsync(settings);
        ThemeState.SetThemeMode(settings.ThemeMode);
        statusMessage = $"Saved at {DateTime.Now:t}";
    }

    private void OnRestoreFileSelected(InputFileChangeEventArgs args)
    {
        selectedBackupFile = args.File;
        selectedBackupFileName = args.File.Name;
        backupStatusMessage = null;
        backupErrorMessage = null;
    }

    private async Task RestoreAsync()
    {
        if (selectedBackupFile is null)
        {
            backupErrorMessage = "Select a JSON backup file first.";
            backupStatusMessage = null;
            return;
        }

        var confirmed = await JS.InvokeAsync<bool>("confirm", "Restore this backup and overwrite the current database?\nThis cannot be undone.");
        if (!confirmed)
        {
            return;
        }

        isRestoring = true;
        backupStatusMessage = null;
        backupErrorMessage = null;

        try
        {
            await using var stream = selectedBackupFile.OpenReadStream(MaxBackupFileSize);
            await DatabaseBackupService.ImportAsync(stream);

            settings = await AppSettingsService.GetAsync();
            ThemeState.SetThemeMode(settings.ThemeMode);
            selectedBackupFile = null;
            selectedBackupFileName = null;
            backupStatusMessage = $"Backup restored at {DateTime.Now:t}.";
        }
        catch (DatabaseBackupException exception)
        {
            backupErrorMessage = exception.Message;
        }
        catch (IOException)
        {
            backupErrorMessage = "The selected backup file is too large or could not be read.";
        }
        finally
        {
            isRestoring = false;
        }
    }
}
