<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class AIController extends Controller
{
    public function process(Request $request)
    {
        $prompt = $request->input('prompt');

        // Kirim prompt ke OpenAI
        $response = Http::withOptions(['verify' => false]) // buat bypass SSL
    ->withToken(env('OPENAI_API_KEY'))
    ->post('https://api.openai.com/v1/chat/completions', [
        'model' => 'gpt-4o-mini',
        'messages' => [
            ['role' => 'system', 'content' => 'Kamu adalah asisten ramah.'],
            ['role' => 'user', 'content' => $prompt],
        ],
        'temperature' => 0.7,
        'max_tokens' => 200,
    ]);

if ($response->successful()) {
    return response()->json([
        'reply' => $response->json()['choices'][0]['message']['content']
    ]);
} else {
    return response()->json([
        'error' => $response->body()
    ], $response->status());
}

    }
}
