@page "/admin/backup"
@using Microsoft.AspNetCore.Authorization
@using EntKube.Web.Services
@rendermode InteractiveServer
@attribute [Authorize(Roles = "Admin")]

@inject BackupService BackupService
@inject AuthenticationStateProvider AuthStateProvider

<PageTitle>Backup & Restore</PageTitle>

<div class="d-flex align-items-center justify-content-between mb-4">
    <div>
        <h1 class="mb-0"><i class="bi bi-arrow-repeat me-2 text-primary"></i>Backup & Restore</h1>
        <p class="text-muted small mb-0 mt-1">Export the full app state or restore from a previous backup.</p>
    </div>
</div>

<div class="row g-4">

    @* ── Export ── *@
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <h5 class="card-title mb-1"><i class="bi bi-download me-2 text-success"></i>Export Backup</h5>
                <p class="text-muted small mb-3">
                    Downloads a <code>.json.gz</code> archive containing all tenants, apps,
                    clusters, deployments, and decrypted secrets. Store it securely — it
                    contains plaintext credentials.
                </p>
                <a href="/api/admin/backup" class="btn btn-success" target="_blank">
                    <i class="bi bi-download me-1"></i>Download Backup
                </a>
            </div>
        </div>
    </div>

    @* ── Restore ── *@
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <h5 class="card-title mb-1"><i class="bi bi-upload me-2 text-warning"></i>Restore from Backup</h5>
                <p class="text-muted small mb-3">
                    Upload a <code>.json.gz</code> backup file produced by this application.
                    Secrets are re-encrypted with the current server's root key on restore.
                </p>

                <div class="mb-3">
                    <InputFile OnChange="OnFileSelected" accept=".gz" class="form-control" />
                </div>

                <div class="form-check mb-3">
                    <input class="form-check-input" type="checkbox" id="wipeCheck" @bind="wipeExisting" />
                    <label class="form-check-label" for="wipeCheck">
                        Wipe all existing data before restoring
                    </label>
                </div>

                @if (wipeExisting)
                {
                    <div class="alert alert-danger py-2 small mb-3">
                        <i class="bi bi-exclamation-triangle-fill me-1"></i>
                        <strong>All current data will be permanently deleted</strong> before the
                        backup is loaded. This cannot be undone. Only proceed on a fresh
                        installation or when you intend a full replacement.
                    </div>
                }

                <button class="btn btn-warning" @onclick="RunRestore"
                        disabled="@(selectedFile is null || restoring)">
                    @if (restoring)
                    {
                        <span class="spinner-border spinner-border-sm me-1" role="status"></span>
                        <span>Restoring…</span>
                    }
                    else
                    {
                        <i class="bi bi-upload me-1"></i>
                        <span>Restore</span>
                    }
                </button>
            </div>
        </div>
    </div>

</div>

@if (!string.IsNullOrEmpty(successMessage))
{
    <div class="alert alert-success mt-4">
        <i class="bi bi-check-circle-fill me-2"></i>@successMessage
    </div>
}

@if (!string.IsNullOrEmpty(errorMessage))
{
    <div class="alert alert-danger mt-4">
        <i class="bi bi-exclamation-triangle-fill me-2"></i>@errorMessage
    </div>
}

@code {
    private IBrowserFile? selectedFile;
    private bool wipeExisting;
    private bool restoring;
    private string? successMessage;
    private string? errorMessage;

    private void OnFileSelected(InputFileChangeEventArgs e)
    {
        selectedFile = e.File;
        successMessage = null;
        errorMessage = null;
    }

    private async Task RunRestore()
    {
        if (selectedFile is null) return;

        restoring = true;
        successMessage = null;
        errorMessage = null;

        try
        {
            // 50 MB ceiling — backups are gzip-compressed JSON so this is generous.
            await using Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 50 * 1024 * 1024);
            await BackupService.ImportAsync(stream, wipeExisting);
            successMessage = $"Restore completed successfully from '{selectedFile.Name}'.";
            selectedFile = null;
        }
        catch (Exception ex)
        {
            errorMessage = $"Restore failed: {ex.Message}";
        }
        finally
        {
            restoring = false;
        }
    }
}
