using ClaudeDo.Installer.Checks;
using ClaudeDo.Installer.Core;
using ClaudeDo.Installer.Pages.DiagnosePage;
using ClaudeDo.Installer.Pages.PathsPage;
using ClaudeDo.Installer.Pages.ServicePage;
using ClaudeDo.Installer.Pages.SystemCheckPage;
using ClaudeDo.Installer.Pages.UiSettingsPage;
using ClaudeDo.Installer.Pages.WelcomePage;
using ClaudeDo.Installer.Tests.Core;

namespace ClaudeDo.Installer.Tests;

public class PageResolverTests
{
    [Fact]
    public void SystemCheckPage_is_in_the_wizard_directly_after_welcome()
    {
        var context = new InstallContext();
        var pages = new IInstallerPage[]
        {
            new WelcomePageViewModel(context),
            new SystemCheckPageViewModel(context, () => throw new InvalidOperationException(), TestClaudeHelpLauncher.Create()),
            new PathsPageViewModel(context),
            new ServicePageViewModel(context),
            new UiSettingsPageViewModel(context),
        };

        var resolver = new PageResolver(pages);
        var wizardPages = resolver.WizardPages;

        var welcomeIndex = wizardPages.ToList().FindIndex(p => p is WelcomePageViewModel);
        var systemCheckIndex = wizardPages.ToList().FindIndex(p => p is SystemCheckPageViewModel);

        Assert.NotEqual(-1, welcomeIndex);
        Assert.Equal(welcomeIndex + 1, systemCheckIndex);
    }

    [Fact]
    public void SystemCheckPage_is_not_shown_in_settings()
    {
        var context = new InstallContext();
        var page = new SystemCheckPageViewModel(context, () => throw new InvalidOperationException(), TestClaudeHelpLauncher.Create());

        Assert.False(page.ShowInSettings);
    }

    [Fact]
    public void DiagnosePage_is_shown_in_settings_but_not_in_the_wizard()
    {
        var context = new InstallContext();
        var page = new DiagnosePageViewModel(context, () => throw new InvalidOperationException(), () => new InstallerWorkerConfig());

        Assert.True(page.ShowInSettings);
        Assert.False(page.ShowInWizard);
    }

    [Fact]
    public void DiagnosePage_appears_in_the_settings_sidebar_after_the_other_settings_pages()
    {
        var context = new InstallContext();
        var pages = new IInstallerPage[]
        {
            new PathsPageViewModel(context),
            new ServicePageViewModel(context),
            new UiSettingsPageViewModel(context),
            new DiagnosePageViewModel(context, () => throw new InvalidOperationException(), () => new InstallerWorkerConfig()),
        };

        var resolver = new PageResolver(pages);
        var settingsPages = resolver.SettingsPages.ToList();

        Assert.Equal(4, settingsPages.Count);
        Assert.IsType<DiagnosePageViewModel>(settingsPages[^1]);
    }
}
