namespace AxCopilot.Services.Agent.SlashCommands;

/// <summary>
/// Phase 22: 슬래시 명령 레지스트리.
/// 내장 명령을 등록하고, 사용자 입력에서 명령을 해석합니다.
/// 내장 명령 > 스킬 명령 우선순위.
/// </summary>
public class SlashCommandRegistry
{
    private readonly Dictionary<string, ISlashCommand> _commands = new(StringComparer.OrdinalIgnoreCase);
    private readonly Dictionary<string, string> _aliases = new(StringComparer.OrdinalIgnoreCase);

    /// <summary>슬래시 명령을 등록합니다.</summary>
    public void Register(ISlashCommand command)
    {
        _commands[command.Name] = command;
        foreach (var alias in command.Aliases)
            _aliases[alias] = command.Name;
    }

    /// <summary>입력 문자열에서 슬래시 명령을 찾습니다. 없으면 null.</summary>
    public ISlashCommand? Resolve(string commandName)
    {
        var name = commandName.TrimStart('/').Split(' ', 2)[0];
        if (_commands.TryGetValue(name, out var cmd)) return cmd;
        if (_aliases.TryGetValue(name, out var canonical))
            return _commands.GetValueOrDefault(canonical);
        return null;
    }

    /// <summary>등록된 모든 명령 목록.</summary>
    public IReadOnlyList<ISlashCommand> GetAll() => _commands.Values.ToList();

    /// <summary>기본 내장 명령을 모두 등록한 레지스트리를 생성합니다.</summary>
    public static SlashCommandRegistry CreateDefault()
    {
        var reg = new SlashCommandRegistry();
        reg.Register(new CompactCommand());
        reg.Register(new ClearCommand());
        reg.Register(new MemoryCommand());
        reg.Register(new ModelCommand());
        reg.Register(new PlanCommand());
        reg.Register(new CommitCommand());
        reg.Register(new ReviewCommand());
        reg.Register(new McpSlashCommand());
        reg.Register(new PermissionsCommand());
        reg.Register(new HooksCommand());
        reg.Register(new ConfigCommand());
        reg.Register(new SkillsCommand());
        reg.Register(new InitCommand());   // Phase 26: 프로젝트 초기화
        reg.Register(new HelpCommand());
        return reg;
    }
}
