namespace AxCopilot.Services.Agent.SlashCommands;

/// <summary>
/// Phase 23: 슬래시 자동완성 제공자.
/// / 입력 시 내장 명령 + 스킬을 결합하여 후보 목록을 반환합니다.
/// </summary>
public class SlashAutoCompleteProvider
{
    private readonly SlashCommandRegistry _commands;

    public SlashAutoCompleteProvider(SlashCommandRegistry commands)
    {
        _commands = commands;
    }

    /// <summary>접두사에 매칭되는 자동완성 후보를 반환합니다.</summary>
    public IReadOnlyList<AutoCompleteItem> GetSuggestions(string input)
    {
        if (string.IsNullOrEmpty(input) || !input.StartsWith('/'))
            return Array.Empty<AutoCompleteItem>();

        var prefix = input.TrimStart('/').ToLowerInvariant();
        var results = new List<AutoCompleteItem>();

        // 1. 내장 명령
        foreach (var cmd in _commands.GetAll())
        {
            if (cmd.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                results.Add(new AutoCompleteItem($"/{cmd.Name}", cmd.Description, cmd.ArgumentHint, true));

            // 별칭도 검색
            foreach (var alias in cmd.Aliases)
            {
                if (alias.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                    results.Add(new AutoCompleteItem($"/{alias}", cmd.Description, cmd.ArgumentHint, true));
            }
        }

        // 2. 스킬
        foreach (var skill in SkillService.Skills)
        {
            if (skill.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                results.Add(new AutoCompleteItem($"/{skill.Name}", skill.Description, null, false));
        }

        return results.OrderByDescending(r => r.IsBuiltIn).ThenBy(r => r.Name).ToList();
    }
}

/// <summary>자동완성 항목.</summary>
public record AutoCompleteItem(string Name, string Description, string? ArgumentHint, bool IsBuiltIn);
