<?php

namespace App\Services\AI;

use Illuminate\Support\Facades\Http;

class OllamaProvider extends BaseAiProvider
{
    public function getIdentifier(): string
    {
        return 'ollama';
    }

    public function generate(string $prompt, array $options = []): array
    {
        $baseUrl = $options['base_url'] ?? $this->config['base_url'] ?? 'http://localhost:11434';
        $model = $options['model'] ?? $this->config['default_model'] ?? 'llama3';
        $instruction = $options['instruction'] ?? $this->config['instruction'] ?? '';

        $startTime = microtime(true);

        try {
            $res = Http::timeout(120)->post("{$baseUrl}/api/generate", [
                'model' => $model,
                'prompt' => $instruction."\n\n".$prompt,
                'stream' => false,
                'options' => [
                    'temperature' => (float) ($options['temperature'] ?? $this->config['temperature'] ?? 0.7),
                    'num_predict' => (int) ($options['max_tokens'] ?? $this->config['max_tokens'] ?? 2000),
                ],
            ]);

            if ($res->failed()) {
                $error = 'Ollama Error: Make sure the server is running and the model is pulled.';
                $this->logUsage([
                    'model' => $model,
                    'prompt' => $prompt,
                    'status' => 'failed',
                    'error' => $error,
                ]);

                return ['success' => false, 'error' => $error];
            }

            $data = $res->json();
            $response = $data['response'];

            $usage = [
                'prompt_tokens' => $data['prompt_eval_count'] ?? 0,
                'completion_tokens' => $data['eval_count'] ?? 0,
                'total_tokens' => ($data['prompt_eval_count'] ?? 0) + ($data['eval_count'] ?? 0),
            ];

            $result = [
                'success' => true,
                'response' => $response,
                'usage' => $usage,
                'model' => $model,
                'latency' => microtime(true) - $startTime,
            ];

            $this->logUsage([
                'model' => $model,
                'prompt' => $prompt,
                'response' => $response,
                'usage' => $usage,
                'status' => 'success',
                'metadata' => ['latency' => $result['latency']],
            ]);

            return $result;

        } catch (\Exception $e) {
            return ['success' => false, 'error' => $e->getMessage()];
        }
    }
}
