using System.Windows.Controls;
using ClaudeDo.Installer.Checks;
using ClaudeDo.Installer.Core;
using ClaudeDo.Installer.Localization;
using CommunityToolkit.Mvvm.ComponentModel;

namespace ClaudeDo.Installer.Pages.DiagnosePage;

/// <summary>
/// Re-runs the same environment checks as the wizard's SystemCheckPage, but against the
/// installed configuration (worker.config.json + the detected install directory) instead of
/// wizard defaults, and nothing here blocks navigation or auto-runs on load.
/// </summary>
public partial class DiagnosePageViewModel : ObservableObject, IInstallerPage
{
    private readonly InstallContext _sharedContext;
    private readonly Func<InstallerWorkerConfig> _loadWorkerConfig;
    private readonly InstallContext _installedContext = new();
    private DiagnosePageView? _view;

    public string Title => TrExtension.Localizer?["installer.diagnose.title"] ?? "Diagnose";
    public string Icon => "";
    public int Order => 5;
    public bool ShowInWizard => false;
    public bool ShowInSettings => true;
    public UserControl View => _view ??= new DiagnosePageView { DataContext = this };

    public CheckListViewModel Checks { get; }

    public DiagnosePageViewModel(InstallContext sharedContext, Func<EnvironmentCheckService> checkServiceFactory, Func<InstallerWorkerConfig> loadWorkerConfig)
    {
        _sharedContext = sharedContext;
        _loadWorkerConfig = loadWorkerConfig;
        Checks = new CheckListViewModel(_installedContext, checkServiceFactory);
    }

    public Task LoadAsync()
    {
        var cfg = _loadWorkerConfig();
        _installedContext.InstallDirectory = _sharedContext.InstallDirectory;
        _installedContext.ClaudeBin = cfg.ClaudeBin;
        _installedContext.SignalRPort = cfg.SignalRPort;
        _installedContext.ExternalMcpPort = _sharedContext.ExternalMcpPort;
        return Task.CompletedTask;
    }

    public Task ApplyAsync() => Task.CompletedTask;

    public bool Validate() => true;
}
