|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Models\Ticket; |
| 6 | +use App\Services\AI\AIService; |
| 7 | +use App\Services\OrganizationContext; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | + |
| 11 | +class AIController extends Controller |
| 12 | +{ |
| 13 | + public function __construct( |
| 14 | + private AIService $aiService, |
| 15 | + private OrganizationContext $organizationContext, |
| 16 | + ) {} |
| 17 | + |
| 18 | + /** |
| 19 | + * Generate suggested replies for a ticket. |
| 20 | + */ |
| 21 | + public function suggest(Request $request, Ticket $ticket): JsonResponse |
| 22 | + { |
| 23 | + $organization = $this->organizationContext->organization(); |
| 24 | + |
| 25 | + if (! $this->aiService->isConfigured($organization)) { |
| 26 | + return response()->json([ |
| 27 | + 'error' => 'AI is niet geconfigureerd. Configureer eerst een AI provider in de instellingen.', |
| 28 | + ], 422); |
| 29 | + } |
| 30 | + |
| 31 | + $settings = $organization->aiSettings; |
| 32 | + |
| 33 | + if (! $settings->suggested_replies_enabled) { |
| 34 | + return response()->json([ |
| 35 | + 'error' => 'Suggesties zijn uitgeschakeld in de AI instellingen.', |
| 36 | + ], 422); |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + $suggestions = $this->aiService->suggestReplies( |
| 41 | + $ticket, |
| 42 | + count: 3, |
| 43 | + user: $request->user() |
| 44 | + ); |
| 45 | + |
| 46 | + return response()->json([ |
| 47 | + 'suggestions' => $suggestions, |
| 48 | + ]); |
| 49 | + } catch (\Exception $e) { |
| 50 | + return response()->json([ |
| 51 | + 'error' => 'Kon geen suggesties genereren: '.$e->getMessage(), |
| 52 | + ], 500); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Refactor/improve reply text. |
| 58 | + */ |
| 59 | + public function refactor(Request $request): JsonResponse |
| 60 | + { |
| 61 | + $validated = $request->validate([ |
| 62 | + 'text' => ['required', 'string', 'min:10', 'max:10000'], |
| 63 | + 'instructions' => ['nullable', 'string', 'max:500'], |
| 64 | + 'ticket_id' => ['nullable', 'integer', 'exists:tickets,id'], |
| 65 | + ]); |
| 66 | + |
| 67 | + $organization = $this->organizationContext->organization(); |
| 68 | + |
| 69 | + if (! $this->aiService->isConfigured($organization)) { |
| 70 | + return response()->json([ |
| 71 | + 'error' => 'AI is niet geconfigureerd. Configureer eerst een AI provider in de instellingen.', |
| 72 | + ], 422); |
| 73 | + } |
| 74 | + |
| 75 | + $settings = $organization->aiSettings; |
| 76 | + |
| 77 | + if (! $settings->reply_refactor_enabled) { |
| 78 | + return response()->json([ |
| 79 | + 'error' => 'Tekst verbeteren is uitgeschakeld in de AI instellingen.', |
| 80 | + ], 422); |
| 81 | + } |
| 82 | + |
| 83 | + $ticket = null; |
| 84 | + if (! empty($validated['ticket_id'])) { |
| 85 | + $ticket = Ticket::find($validated['ticket_id']); |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + $refactored = $this->aiService->refactorReply( |
| 90 | + $validated['text'], |
| 91 | + $organization, |
| 92 | + $validated['instructions'] ?? null, |
| 93 | + $request->user(), |
| 94 | + $ticket |
| 95 | + ); |
| 96 | + |
| 97 | + return response()->json([ |
| 98 | + 'text' => $refactored, |
| 99 | + ]); |
| 100 | + } catch (\Exception $e) { |
| 101 | + return response()->json([ |
| 102 | + 'error' => 'Kon tekst niet verbeteren: '.$e->getMessage(), |
| 103 | + ], 500); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Check if AI is configured and available. |
| 109 | + */ |
| 110 | + public function status(): JsonResponse |
| 111 | + { |
| 112 | + $organization = $this->organizationContext->organization(); |
| 113 | + $settings = $organization->aiSettings; |
| 114 | + |
| 115 | + return response()->json([ |
| 116 | + 'configured' => $this->aiService->isConfigured($organization), |
| 117 | + 'suggested_replies_enabled' => $settings?->suggested_replies_enabled ?? false, |
| 118 | + 'reply_refactor_enabled' => $settings?->reply_refactor_enabled ?? false, |
| 119 | + ]); |
| 120 | + } |
| 121 | +} |
0 commit comments