using DragonISO.App.Services;

namespace DragonISO.App.ViewModels;

/// <summary>
/// Persistent banner shown when the launch-time update check finds a newer
/// release. Stays visible until the operator clicks "Get update" (which opens
/// the releases page) or "Dismiss" (which hides until next launch).
///
/// Distinct from <see cref="AlertBannerViewModel"/> because that one's tied to
/// engine alerts and would force us to leak update concerns into the engine
/// layer; the update banner is purely a host-shell concern.
/// </summary>
public sealed class UpdateBannerViewModel : ObservableObject
{
    private bool _isVisible;
    private string _latestVersion = string.Empty;
    private string _currentVersion = string.Empty;

    public bool IsVisible
    {
        get => _isVisible;
        private set => SetField(ref _isVisible, value);
    }

    public string LatestVersion
    {
        get => _latestVersion;
        private set => SetField(ref _latestVersion, value);
    }

    public string CurrentVersion
    {
        get => _currentVersion;
        private set => SetField(ref _currentVersion, value);
    }

    /// <summary>Friendly composite "1.0.0 â†’ 1.1.0" string for the banner.</summary>
    public string Message => $"Update available â€” v{_currentVersion} â†’ {_latestVersion}";

    public RelayCommand OpenReleasePageCommand { get; }
    public RelayCommand DismissCommand { get; }

    public UpdateBannerViewModel()
    {
        OpenReleasePageCommand = new RelayCommand(() =>
        {
            UpdateChecker.OpenReleasesPage();
            IsVisible = false;
        });
        DismissCommand = new RelayCommand(() => IsVisible = false);
    }

    /// <summary>
    /// Show the banner with a "you're on X, latest is Y" message. Idempotent:
    /// re-showing while already visible just refreshes the version strings.
    /// </summary>
    public void Show(string current, string latest)
    {
        CurrentVersion = current;
        LatestVersion = latest;
        OnPropertyChanged(nameof(Message));
        IsVisible = true;
    }
}
