using System;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HC_APTBS.Services;
using HC_APTBS.ViewModels.Dialogs;
using HC_APTBS.Views.Dialogs;
namespace HC_APTBS.ViewModels
{
///
/// ViewModel for the AuthGateView wrapper control that hides write-capable
/// content behind an operator authentication dialog.
///
/// Used by the Pump page on the Adaptation and Unlock sub-sections per
/// docs/ui-structure.md §3.d/§3.e (write actions require authentication).
///
public sealed partial class AuthGateViewModel : ObservableObject
{
private readonly IConfigurationService _config;
private readonly ILocalizationService _loc;
/// Initialises the gate; starts in the locked state.
public AuthGateViewModel(IConfigurationService config, ILocalizationService loc)
{
_config = config;
_loc = loc;
}
// ── State ─────────────────────────────────────────────────────────────────
/// True once the operator has successfully authenticated.
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(AuthenticateCommand))]
[NotifyCanExecuteChangedFor(nameof(LockCommand))]
private bool _isAuthenticated;
/// The currently authenticated username (empty when locked).
[ObservableProperty] private string _authenticatedUser = string.Empty;
// ── Commands ──────────────────────────────────────────────────────────────
/// Opens the UserCheck dialog; flips to authenticated on success.
[RelayCommand(CanExecute = nameof(CanAuthenticate))]
private void Authenticate()
{
var vm = new UserCheckViewModel(_config, _loc, AuthenticatedUser);
var dlg = new UserCheckDialog(vm) { Owner = Application.Current.MainWindow };
dlg.ShowDialog();
if (!vm.Accepted) return;
AuthenticatedUser = vm.AuthenticatedUser;
IsAuthenticated = true;
}
private bool CanAuthenticate() => !IsAuthenticated;
/// Re-locks the gate (e.g. after finishing a sensitive operation).
[RelayCommand(CanExecute = nameof(CanLock))]
private void Lock()
{
IsAuthenticated = false;
AuthenticatedUser = string.Empty;
}
private bool CanLock() => IsAuthenticated;
}
}