using System.Security.Cryptography;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
using ScadaLink.CentralUI.Auth;
using ScadaLink.Commons.Entities.ExternalSystems;
using ScadaLink.Commons.Entities.InboundApi;
using ScadaLink.Commons.Entities.Notifications;
using ScadaLink.Commons.Entities.Scripts;
using ScadaLink.Commons.Entities.Templates;
using ScadaLink.Commons.Interfaces.Repositories;
using ScadaLink.Commons.Interfaces.Transport;
using ScadaLink.Commons.Types.Transport;
using ScadaLink.Transport;
using ScadaLink.Transport.Export;
namespace ScadaLink.CentralUI.Components.Pages.Design;
///
/// Code-behind for the TransportExport wizard (Transport feature, Task T21).
///
/// Four-step state machine:
///
/// - — pick templates + flat artifact lists.
/// - — show resolved closure (seed + auto-included).
/// - — passphrase + secret-count warning, or explicit unencrypted opt-out.
/// - — call , render the file via JS interop.
///
///
/// The wizard transitions are linear; "Back" returns to the previous step
/// without clearing selection state. "Done" on Step 4 resets to Step 1 fresh.
///
/// SourceEnvironment is sourced from
/// (bound from ScadaLink:Transport:SourceEnvironment) so a multi-cluster
/// deployment can label its bundles distinctly. Defaults to "scadalink".
///
public partial class TransportExport : ComponentBase
{
public enum ExportWizardStep
{
Select = 1,
Review = 2,
Encrypt = 3,
Download = 4,
}
// ---- Injected services ----
[Inject] private IBundleExporter BundleExporter { get; set; } = default!;
[Inject] private ITemplateEngineRepository TemplateRepo { get; set; } = default!;
[Inject] private IExternalSystemRepository ExternalRepo { get; set; } = default!;
[Inject] private INotificationRepository NotificationRepo { get; set; } = default!;
[Inject] private IInboundApiRepository InboundApiRepo { get; set; } = default!;
[Inject] private DependencyResolver DepResolver { get; set; } = default!;
[Inject] private IJSRuntime JS { get; set; } = default!;
[Inject] private AuthenticationStateProvider Auth { get; set; } = default!;
[Inject] private IOptions TransportOptions { get; set; } = default!;
// ---- Wizard state ----
private ExportWizardStep _step = ExportWizardStep.Select;
private bool _loading = true;
private string? _errorMessage;
// ---- Step 1: source data ----
private List _templates = new();
private List _folders = new();
private List _sharedScripts = new();
private List _externalSystems = new();
private List _dbConnections = new();
private List _notificationLists = new();
private List _smtpConfigs = new();
private List _apiKeys = new();
private List _apiMethods = new();
// ---- Step 1: selection state ----
// TemplateFolderTree uses string keys ("t:{id}", "f:{id}") via TemplateTreeNode.Key.
// Templates are selected via the tree; the other artifacts use flat checkbox lists
// backed by integer-id HashSets so wiring is uniform across categories.
private readonly HashSet