<?php

namespace App\Actions\Qwen;

use GuzzleHttp\Client;

class AiTextGeneration
{
    protected $client;
    protected $defaultModel = 'qwen-flash';

    public function __construct()
    {
        $this->client = new Client([
            'base_uri' => 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/',
            'headers' => [
                'Authorization' => 'sk-7f92cfeffa6d4c89a227d914589f8c92',
                'Content-Type' => 'application/json',
            ],
        ]);
    }

    public function execute($message, $model = null)
    {
        $response = $this->client->post('chat/completions', [
            'json' => [
                "model" => $model ?? $this->defaultModel,
                "messages" => [
                    [
                        "role" => "user",
                        "content" => $message,
                    ],
                ],
                "stream" => false,
                "top_p" => 0.8,
                "temperature" => 0.7,
                "enable_thinking" => false,
                "thinking_budget" => 4000,
                "result_format" => "message",
            ],
        ]);

        return json_decode($response->getBody()->getContents());
    }

    public function getAvailableModels()
    {
        return [
            'qwen-plus',
            'qwen-plus-latest',
            'qwen3-max',
            'qwen-flash',
            'qwen3-coder-plus',
        ];
    }
}
