using ClawdDotNet.Core.Config;

namespace ClawdDotNet.Core.Tools;

public sealed class ToolRegistry
{
    private readonly Dictionary<string, IAgentTool> _tools = new();
    private readonly Lock _lock = new();

    public void Register(IAgentTool tool)
    {
        lock (_lock)
            _tools[tool.Name] = tool;
    }

    public IAgentTool? Get(string name)
    {
        lock (_lock)
            return _tools.GetValueOrDefault(name);
    }

    public IReadOnlyList<IAgentTool> GetForAgent(AgentConfig agent)
    {
        lock (_lock)
            return _tools.Values
                .Where(t => agent.Tools.ContainsKey(t.Name))
                .ToList();
    }

    public IReadOnlyList<IAgentTool> GetAll()
    {
        lock (_lock)
            return _tools.Values.ToList();
    }

    public IReadOnlyList<(IAgentTool Tool, IToolJobProvider Provider)> GetJobProviders()
    {
        lock (_lock)
            return _tools.Values
                .OfType<IToolJobProvider>()
                .Select(p => ((IAgentTool)p, p))
                .ToList();
    }
}
