<?php
declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/AzureOpenAiGateway.php';
require_once __DIR__ . '/DbnBedrockGateway.php';
require_once __DIR__ . '/DbnBedrockModelRouter.php';

/**
 * Single choke point for LLM gateway selection.
 * When DBN_BEDROCK_ENABLED=false (default), always returns Azure — zero risk.
 * When true, routes through LiteLLM on Colin (same path nova-lite already uses).
 * AWS credentials live only in LiteLLM config — no keys needed in .env here.
 * Rollback: flip DBN_BEDROCK_ENABLED=false in .env — no code push needed.
 */
final class DbnGatewayFactory
{
    public static function bedrockEnabled(): bool
    {
        return dbnToolsEnv('DBN_BEDROCK_ENABLED', 'false') === 'true';
    }

    /**
     * Returns a Bedrock gateway configured for the given LiteLLM model name,
     * or the default from DBN_BEDROCK_CHAT_MODEL. Falls back to Azure when disabled.
     */
    public static function make(?string $modelName = null): DbnAzureOpenAiGateway|DbnBedrockGateway
    {
        if (!self::bedrockEnabled()) {
            return new DbnAzureOpenAiGateway();
        }

        return new DbnBedrockGateway([
            'chat_model_name' => $modelName
                ?? (string)dbnToolsEnv('DBN_BEDROCK_CHAT_MODEL', DbnBedrockModelRouter::LITELLM_SONNET),
        ]);
    }

    /**
     * Returns a gateway pre-configured with the best model for the given tool and tier.
     *
     * Three-tier routing:
     *  - Azure-pinned tools (redact, translate, timeline, search-legal) → always Azure, Bedrock flag ignored.
     *  - Haiku tools (ask, summarize, timeline-deep, citations) → Haiku when Bedrock on, Azure when off.
     *  - Sonnet tools (korrespond, legal-analysis, deep-research, etc.) → Sonnet when on, Azure when off.
     */
    public static function makeForTool(
        string $tool,
        string $tier = 'free'
    ): DbnAzureOpenAiGateway|DbnBedrockGateway {
        $route = DbnBedrockModelRouter::routeForTool($tool, $tier);

        // Azure-pinned tools bypass the Bedrock flag entirely
        if ($route['gateway'] === 'azure') {
            return new DbnAzureOpenAiGateway();
        }

        // Bedrock tools fall back to Azure when disabled
        if (!self::bedrockEnabled()) {
            return new DbnAzureOpenAiGateway();
        }

        return self::make($route['model']);
    }
}
