using System.Collections.ObjectModel;
using ClaudeDo.Data.Models;
using ClaudeDo.Ui.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace ClaudeDo.Ui.ViewModels.Modals;

public sealed partial class ListSettingsModalViewModel : ViewModelBase
{
    private readonly WorkerClient _worker;

    public string ListId { get; set; } = "";

    [ObservableProperty] private string _name = "";
    [ObservableProperty] private string _workingDir = "";
    [ObservableProperty] private string _defaultCommitType = CommitTypeRegistry.DefaultType;

    [ObservableProperty] private string _selectedModel = ModelRegistry.ListDefaultSentinel;
    [ObservableProperty] private string _systemPrompt = "";
    [ObservableProperty] private AgentInfo? _selectedAgent;

    public ObservableCollection<string> ModelOptions { get; } = new(
        new[] { ModelRegistry.ListDefaultSentinel }.Concat(ModelRegistry.Aliases));

    public ObservableCollection<string> CommitTypeOptions { get; } = new(CommitTypeRegistry.Types);

    public ObservableCollection<AgentInfo> Agents { get; } = new();

    public Action? CloseAction { get; set; }

    public ListSettingsModalViewModel(WorkerClient worker)
    {
        _worker = worker;
    }

    public async Task LoadAsync(
        string listId,
        string name,
        string? workingDir,
        string defaultCommitType,
        CancellationToken ct = default)
    {
        ListId = listId;
        Name = name;
        WorkingDir = workingDir ?? "";
        DefaultCommitType = string.IsNullOrWhiteSpace(defaultCommitType) ? CommitTypeRegistry.DefaultType : defaultCommitType;

        Agents.Clear();
        Agents.Add(new AgentInfo("(none)", "", ""));
        var agents = await _worker.GetAgentsAsync();
        foreach (var a in agents) Agents.Add(a);

        var config = await _worker.GetListConfigAsync(listId);
        SelectedModel = string.IsNullOrWhiteSpace(config?.Model) ? ModelRegistry.ListDefaultSentinel : config!.Model!;
        SystemPrompt = config?.SystemPrompt ?? "";
        SelectedAgent = string.IsNullOrWhiteSpace(config?.AgentPath)
            ? Agents[0]
            : (Agents.FirstOrDefault(a => a.Path == config!.AgentPath) ?? Agents[0]);
    }

    [RelayCommand]
    private async Task SaveAsync()
    {
        var model = SelectedModel == ModelRegistry.ListDefaultSentinel ? null : SelectedModel;
        var sp = string.IsNullOrWhiteSpace(SystemPrompt) ? null : SystemPrompt;
        var ap = SelectedAgent is null || string.IsNullOrWhiteSpace(SelectedAgent.Path) ? null : SelectedAgent.Path;

        await _worker.UpdateListAsync(new UpdateListDto(
            ListId,
            string.IsNullOrWhiteSpace(Name) ? "Untitled" : Name,
            string.IsNullOrWhiteSpace(WorkingDir) ? null : WorkingDir,
            DefaultCommitType));

        await _worker.UpdateListConfigAsync(new UpdateListConfigDto(
            ListId, model, sp, ap));

        CloseAction?.Invoke();
    }

    [RelayCommand]
    private void Cancel() => CloseAction?.Invoke();

    [RelayCommand]
    private void ResetAgentSettings()
    {
        SelectedModel = ModelRegistry.ListDefaultSentinel;
        SystemPrompt = "";
        SelectedAgent = Agents.Count > 0 ? Agents[0] : null;
    }
}
