<?php
namespace boru\ai\Mcp;

class RoutingMcpClientFactory implements McpClientFactoryInterface
{
    /** @var array */
    protected $factories = array();

    public function register($transport, McpClientFactoryInterface $factory)
    {
        $transport = strtolower(trim((string)$transport));
        if ($transport === '') {
            throw new \InvalidArgumentException('MCP client factory transport is required');
        }

        $this->factories[$transport] = $factory;
        return $this;
    }

    public function has($transport)
    {
        return isset($this->factories[strtolower(trim((string)$transport))]);
    }

    public function makeClient(McpServerConfig $serverConfig)
    {
        $transport = strtolower(trim((string)$serverConfig->transport()));
        if ($transport === '') {
            throw new \RuntimeException('MCP server transport not configured for server "' . $serverConfig->name() . '"');
        }

        if (!$this->has($transport)) {
            throw new \RuntimeException('No MCP client factory registered for transport "' . $transport . '"');
        }

        return $this->factories[$transport]->makeClient($serverConfig);
    }
}
