using System.Collections.ObjectModel;
using ClaudeDo.Data.Models;
using ClaudeDo.Localization;
using ClaudeDo.Ui.Services;
using ClaudeDo.Ui.ViewModels.Agent;
using CommunityToolkit.Mvvm.ComponentModel;

namespace ClaudeDo.Ui.ViewModels.Modals.Settings;

public sealed partial class GeneralSettingsTabViewModel : ViewModelBase
{
    private readonly ILocalizer? _localizer;
    private readonly Action<string>? _persist;

    [ObservableProperty] private string _defaultClaudeInstructions = "";
    [ObservableProperty] private string _defaultModel = ModelRegistry.DefaultAlias;
    [ObservableProperty] private int _defaultMaxTurns = 100;
    [ObservableProperty] private string _defaultPermissionMode = PermissionModeRegistry.DefaultMode;
    [ObservableProperty] private int _maxParallelExecutions = 1;
    // Newline-separated path prefixes excluded from the weekly report.
    [ObservableProperty] private string _reportExcludedPaths = @"C:\Private";
    // 0=Sunday..6=Saturday (System.DayOfWeek); default Wednesday.
    [ObservableProperty] private int _standupWeekday = (int)DayOfWeek.Wednesday;

    public IReadOnlyList<string> Models { get; } = ModelRegistry.Aliases;
    public IReadOnlyList<string> PermissionModes { get; } = PermissionModeRegistry.Modes;

    public ObservableCollection<SelectableSkillViewModel> SessionSkills { get; } = new();

    public GeneralSettingsTabViewModel() { }

    public GeneralSettingsTabViewModel(ILocalizer localizer, Action<string> persist)
    {
        _localizer = localizer;
        _persist = persist;
        Languages = localizer.AvailableLanguages;
        _selectedLanguage = Languages.FirstOrDefault(l => l.Code == localizer.CurrentCode);
    }

    public IReadOnlyList<LanguageOption> Languages { get; } = Array.Empty<LanguageOption>();

    [ObservableProperty] private LanguageOption? _selectedLanguage;

    partial void OnSelectedLanguageChanged(LanguageOption? value)
    {
        if (value is null || _localizer is null) return;
        _localizer.SetLanguage(value.Value.Code);
        _persist?.Invoke(value.Value.Code);
    }

    public string? Validate()
    {
        if (DefaultMaxTurns < 1 || DefaultMaxTurns > 200)
            return "Max turns must be between 1 and 200.";
        if (MaxParallelExecutions < 1 || MaxParallelExecutions > 20)
            return "Max parallel executions must be between 1 and 20.";
        return null;
    }

    /// <summary>Loads the installed-skills checkbox list, reflecting the given selection.</summary>
    public async Task LoadSessionSkillsAsync(IWorkerClient worker, IReadOnlyCollection<string>? selected)
    {
        var installed = await worker.GetSessionSkillsAsync();
        var selectedSet = selected is null ? new HashSet<string>() : new HashSet<string>(selected);
        SessionSkills.Clear();
        foreach (var s in installed)
            SessionSkills.Add(new SelectableSkillViewModel(s.Name, s.Description, selectedSet.Contains(s.Name)));
    }

    public List<string>? SelectedSessionSkillNames()
    {
        var names = SessionSkills.Where(s => s.IsSelected).Select(s => s.Name).ToList();
        return names.Count == 0 ? null : names;
    }
}
