<?php

declare(strict_types=1);

namespace LolaCore\LLM;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use WordPress\AiClient\AiClient;
use LolaCore\LLM\Providers\OpenAI\OpenAIProvider;
use LolaCore\LLM\Providers\DeepSeek\DeepSeekProvider;
use LolaCore\LLM\Providers\Anthropic\Provider\AnthropicProvider;

/**
 * Registers LLM Providers with the native WP 7.0 AI Client.
 */
class ProviderRegistrar
{
    /**
     * Hooks into the early init phase.
     */
    public function register(): void
    {
        // Hook early (priority 5) before AiClient initializes.
        add_action('init', [$this, 'registerProviders'], 5);
    }

    /**
     * Registers missing providers.
     */
    public function registerProviders(): void
    {
        if (!class_exists('WordPress\AiClient\AiClient')) {
            return;
        }

        $registry = AiClient::defaultRegistry();

        $providers = [
            'openai'    => OpenAIProvider::class,
            'deepseek'  => DeepSeekProvider::class,
            'anthropic' => AnthropicProvider::class,
        ];

        foreach ($providers as $id => $class) {
            if (!$registry->hasProvider($id)) {
                $registry->registerProvider($class);
            }
        }
    }
}
