using System.Security.Cryptography;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Scripts;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
using ZB.MOM.WW.ScadaBridge.Transport;
using ZB.MOM.WW.ScadaBridge.Transport.Export;
namespace ZB.MOM.WW.ScadaBridge.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 ScadaBridge:Transport:SourceEnvironment) so a multi-cluster
/// deployment can label its bundles distinctly. Defaults to "scadabridge".
///
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 ISiteRepository SiteRepo { 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();
// Inbound API keys are not transported between environments (re-arch C4); only methods.
private List _apiMethods = new();
// M8 (E1): site/instance-scoped export. Sites are listed flat; each site's
// instances hang off it (loaded eagerly via ISiteRepository.GetInstancesBySiteIdAsync)
// so the operator can pick a whole site or drill into individual instances.
private List _sites = new();
private readonly Dictionary> _instancesBySiteId = 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