<?php

namespace App\Service\AI;

use GeminiAPI\Client;
use GeminiAPI\Resources\Parts\TextPart;

class GeminiClient
{
    private ?Client $client = null;
    
    public function __construct(
        private readonly string $apiKey = "AIzaSyDTPJERlUC47bcvhZU51Lwpqb1uxXS8SIg", // Keeping the hardcoded key as in original command for now, but should be env var
        ?Client $client = null
    ) {
        $this->client = $client;
    }

    public function generateFriendlyMessage(string $rawMessage): ?string
    {
        try {
            if (!$this->client) {
                $this->client = new Client($this->apiKey);
            }
            
            $model = 'gemini-3-pro-preview';
            $prompt = "Tu es un expert en communication web pour Ludik Event. Ta mission est de transformer
                       un message de commit technique en une note de mise à jour élégante pour ton client.
                       MESSAGE TECHNIQUE : \"$rawMessage\"
                       DIRECTIVES : Court, positif, pas de 'Voici la phrase', uniquement le résultat final.";

            $response = $this->client->withV1BetaVersion()->generativeModel($model)->generateContent(new TextPart($prompt));
            
            return $response->text() ? trim($response->text()) : null;
            
        } catch (\Exception $e) {
            // Log or ignore? Command just ignored it.
            return null;
        }
    }
}

