package cli

import (
	"log/slog"
	"strings"

	"github.com/pardnchiu/agenvoy/internal/agents/provider/claude"
	"github.com/pardnchiu/agenvoy/internal/agents/provider/compat"
	"github.com/pardnchiu/agenvoy/internal/agents/provider/copilot"
	"github.com/pardnchiu/agenvoy/internal/agents/provider/deepseek"
	"github.com/pardnchiu/agenvoy/internal/agents/provider/gemini"
	"github.com/pardnchiu/agenvoy/internal/agents/provider/grok"
	"github.com/pardnchiu/agenvoy/internal/agents/provider/nvidia"
	"github.com/pardnchiu/agenvoy/internal/agents/provider/openai"
	openaicodex "github.com/pardnchiu/agenvoy/internal/agents/provider/openaiCodex"
	agentTypes "github.com/pardnchiu/agenvoy/internal/agents/types"
)

func SelectAgent(model string) agentTypes.Agent {
	agentMap := map[string]func(string) (agentTypes.Agent, error){
		"copilot":  func(m string) (agentTypes.Agent, error) { return copilot.New(m) },
		"openai":   func(m string) (agentTypes.Agent, error) { return openai.New(m) },
		"codex":    func(m string) (agentTypes.Agent, error) { return openaicodex.New(m) },
		"compat":   func(m string) (agentTypes.Agent, error) { return compat.New(m) },
		"claude":   func(m string) (agentTypes.Agent, error) { return claude.New(m) },
		"gemini":   func(m string) (agentTypes.Agent, error) { return gemini.New(m) },
		"nvidia":   func(m string) (agentTypes.Agent, error) { return nvidia.New(m) },
		"grok":     func(m string) (agentTypes.Agent, error) { return grok.New(m) },
		"deepseek": func(m string) (agentTypes.Agent, error) { return deepseek.New(m) },
	}
	provider, _, _ := strings.Cut(model, "@")
	name, _, _ := strings.Cut(provider, "[")
	fn, ok := agentMap[name]
	if !ok {
		return nil
	}
	agent, err := fn(model)
	if err != nil {
		slog.Warn("newAgentFromModel",
			slog.String("error", err.Error()))
		return nil
	}
	return agent
}
