<?php

declare(strict_types=1);

namespace ImovelZapAi\AiGateway\Infrastructure\Providers;

use ImovelZapAi\AiGateway\Application\Data\AiCompletionRequest;
use ImovelZapAi\AiGateway\Application\Data\AiProviderResult;
use ImovelZapAi\AiGateway\Application\Data\AiTokenUsageData;
use ImovelZapAi\AiGateway\Application\Ports\AiProviderInterface;
use ImovelZapAi\AiGateway\Domain\Enums\AiProvider;
use ImovelZapAi\AiGateway\Domain\Exceptions\AiProviderException;

final class NullAiProvider implements AiProviderInterface
{
    public function provider(): AiProvider
    {
        return AiProvider::Null;
    }

    public function isConfigured(): bool
    {
        return true;
    }

    public function complete(AiCompletionRequest $request, string $model): AiProviderResult
    {
        $lastUserMessage = '';

        foreach (array_reverse($request->messages) as $message) {
            if ($message->role === 'user') {
                $lastUserMessage = $message->content;
                break;
            }
        }

        return new AiProviderResult(
            text: '[IA desabilitada] '.$lastUserMessage,
            model: $model,
            provider: AiProvider::Null,
            tokenUsage: new AiTokenUsageData(0, 0),
        );
    }
}
