using System.IO;
using System.Windows.Controls;
using ClaudeDo.Installer.Core;
using ClaudeDo.Installer.Localization;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Win32;

namespace ClaudeDo.Installer.Pages.WelcomePage;

public partial class WelcomePageViewModel : ObservableObject, IInstallerPage
{
    private readonly InstallContext _context;
    private WelcomePageView? _view;

    public string Title => TrExtension.Localizer?["installer.welcome.title"] ?? "Welcome";
    public string Icon => "\uE80F";
    public int Order => 0;
    public bool ShowInWizard => true;
    public bool ShowInSettings => false;
    public UserControl View => _view ??= new WelcomePageView { DataContext = this };

    [ObservableProperty] private string _installDirectory = @"C:\Program Files\ClaudeDo";
    [ObservableProperty] private string? _installError;
    [ObservableProperty] private string _heading = "Install ClaudeDo";
    [ObservableProperty] private string _subheading = "Set the installation directory and continue.";
    [ObservableProperty] private bool _installDirEditable = true;
    [ObservableProperty] private bool _registerMcp = true;

    public WelcomePageViewModel(InstallContext context)
    {
        _context = context;
    }

    public Task LoadAsync()
    {
        InstallDirectory = string.IsNullOrEmpty(_context.InstallDirectory)
            ? @"C:\Program Files\ClaudeDo"
            : _context.InstallDirectory;

        var loc = TrExtension.Localizer;
        switch (_context.Mode)
        {
            case InstallerMode.FreshInstall:
                Heading = loc?["installer.welcome.heading"] ?? "Install ClaudeDo";
                Subheading = loc?["installer.welcome.subheading"] ?? "Choose where to install ClaudeDo, then click Next.";
                InstallDirEditable = true;
                break;

            case InstallerMode.Update:
                Heading = $"Update ClaudeDo  {_context.InstalledVersion ?? "?"}  ->  {_context.LatestVersion ?? "?"}";
                Subheading = loc?["installer.welcome.updateSubheading"] ?? "Your tasks, config, and database will be preserved. Click Next to continue.";
                InstallDirEditable = false; // stay where we were installed
                break;

            default:
                // Config and any future modes should never reach the wizard; guard loudly if they do.
                throw new InvalidOperationException(
                    $"WelcomePage is not valid for installer mode {_context.Mode}");
        }

        return Task.CompletedTask;
    }

    public Task ApplyAsync()
    {
        _context.InstallDirectory = InstallDirectory;
        _context.RegisterMcpWithClaude = RegisterMcp;
        return Task.CompletedTask;
    }

    public bool Validate()
    {
        if (string.IsNullOrWhiteSpace(InstallDirectory))
        {
            InstallError = "Install directory is required";
            return false;
        }
        InstallError = null;
        return true;
    }

    [RelayCommand]
    private void BrowseInstall()
    {
        if (!InstallDirEditable) return;
        var dialog = new OpenFolderDialog { Title = "Select installation directory" };
        if (dialog.ShowDialog() == true)
            InstallDirectory = dialog.FolderName;
    }
}
