package registry_test

import (
	"context"
	"encoding/json"
	"testing"

	"github.com/mathiasbq/supervisor/internal/registry"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type stubSkill struct {
	name string
}

func (s stubSkill) Name() string { return s.name }
func (s stubSkill) Tools() []registry.ToolDef {
	return []registry.ToolDef{{
		Name:        s.name + "_tool",
		Description: "stub tool",
		InputSchema: json.RawMessage(`{"type":"object","properties":{}}`),
	}}
}
func (s stubSkill) Handle(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, error) {
	return json.RawMessage(`{"ok":true}`), nil
}

func TestRegistryRoutes(t *testing.T) {
	r := registry.New()
	r.Register(stubSkill{name: "tdd"})

	result, err := r.Dispatch(context.Background(), "tdd_tool", json.RawMessage(`{}`))
	require.NoError(t, err)
	assert.JSONEq(t, `{"ok":true}`, string(result))
}

func TestRegistryUnknownTool(t *testing.T) {
	r := registry.New()
	_, err := r.Dispatch(context.Background(), "unknown_tool", json.RawMessage(`{}`))
	assert.ErrorContains(t, err, "unknown tool")
}

func TestRegistryListsTools(t *testing.T) {
	r := registry.New()
	r.Register(stubSkill{name: "tdd"})

	tools := r.Tools()
	require.Len(t, tools, 1)
	assert.Equal(t, "tdd_tool", tools[0].Name)
}
