<?php

namespace App\Services\AI;

use Illuminate\Support\Facades\Http;

class OpenAiCompatibleProvider extends BaseAiProvider
{
    protected $identifier;

    protected $baseUrl;

    public function __construct(string $identifier, string $baseUrl, array $config = [])
    {
        parent::__construct($config);
        $this->identifier = $identifier;
        $this->baseUrl = $baseUrl;
    }

    public function getIdentifier(): string
    {
        return $this->identifier;
    }

    public function generate(string $prompt, array $options = []): array
    {
        $key = $options['key'] ?? $this->config['key'] ?? null;
        if (! $key) {
            return ['success' => false, 'error' => ucfirst($this->identifier).' API Key not configured.'];
        }

        $model = $options['model'] ?? $this->config['default_model'] ?? null;
        $instruction = $options['instruction'] ?? $this->config['instruction'] ?? '';

        $startTime = microtime(true);

        try {
            $res = Http::timeout(60)->withToken($key)
                ->post($this->baseUrl, [
                    'model' => $model,
                    'messages' => [
                        ['role' => 'system', 'content' => $instruction],
                        ['role' => 'user', 'content' => $prompt],
                    ],
                    'temperature' => (float) ($options['temperature'] ?? $this->config['temperature'] ?? 0.7),
                    'max_tokens' => (int) ($options['max_tokens'] ?? $this->config['max_tokens'] ?? 2000),
                ]);

            if ($res->failed()) {
                $error = $res->json()['error']['message'] ?? $res->json()['message'] ?? ucfirst($this->identifier).' Error';
                $this->logUsage([
                    'model' => $model,
                    'prompt' => $prompt,
                    'status' => 'failed',
                    'error' => $error,
                ]);

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

            $data = $res->json();
            $response = $data['choices'][0]['message']['content'];
            $usage = $data['usage'] ?? [
                'prompt_tokens' => 0,
                'completion_tokens' => 0,
                'total_tokens' => 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()];
        }
    }
}
