Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
FlyAIAdminController
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkOutputPrompt
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Http\Controllers\v2\Admin;
4
5use App\Http\Controllers\Controller;
6use App\Http\Requests\v2\FlyAI\CheckOutputPromptFormRequest;
7use App\Services\FlyMsgAI\FlyMsgAIService;
8use Illuminate\Http\JsonResponse;
9
10/**
11 * Admin controller for testing and debugging AI prompts.
12 *
13 * Provides endpoints to inspect assembled prompts and AI output
14 * without going through the full user-facing flow or tracking events.
15 * Only accessible by users with the VENGRESO_ADMIN role.
16 */
17class FlyAIAdminController extends Controller
18{
19    public function __construct(
20        private readonly FlyMsgAIService $aiService
21    ) {}
22
23    /**
24     * Check the output prompt for a given AI feature.
25     *
26     * Builds the prompt using the same logic as the user-facing endpoints
27     * and optionally calls the AI API. When dry_run is true, only the
28     * assembled prompt and config are returned without making an API call.
29     *
30     * @param  CheckOutputPromptFormRequest  $request  Validated request with feature params
31     *
32     * @response 200 {
33     *   "result": {
34     *     "feature": "flyengage",
35     *     "prompt": "<assembled prompt text>",
36     *     "response": "<AI response or null if dry_run>",
37     *     "config": {
38     *       "model": "gemini-2.5-flash",
39     *       "temperature": 1.0,
40     *       "max_tokens": 4096,
41     *       "top_p": 0.95
42     *     },
43     *     "dry_run": true
44     *   }
45     * }
46     */
47    public function checkOutputPrompt(CheckOutputPromptFormRequest $request): JsonResponse
48    {
49        $validated = $request->validated();
50        $user = $request->user();
51        $feature = $validated['feature'];
52        $dryRun = $validated['dry_run'] ?? false;
53        $aiPromptId = $validated['prompt_id'];
54
55        if ($feature === 'flyengage') {
56            $context = $validated['context'];
57            $context['post']['content'] = strip_tags($context['post']['content'] ?? '');
58
59            $result = $this->aiService->engageGenerate(
60                $aiPromptId,
61                $context,
62                $validated['uniqueId'] ?? 'admin-check',
63                $user,
64                $validated['include_hashtags'],
65                $validated['include_emojis'],
66                $validated['persona_id'] ?? null,
67                $validated['prompt_tone_id'] ?? null,
68                $validated['additional_instructions'] ?? null,
69                $validated['custom_prompt_id'] ?? null,
70                $validated['is_regenerate'] ?? false,
71                $validated['is_reply'] ?? false,
72                $dryRun
73            );
74        } else {
75            $result = $this->aiService->postGenerate(
76                $aiPromptId,
77                $validated['youtube_url'] ?? null,
78                $validated['blog_url'] ?? null,
79                $validated['uniqueId'] ?? 'admin-check',
80                $user,
81                $validated['include_hashtags'],
82                $validated['include_emojis'],
83                $validated['prompt_language_id'],
84                $validated['length_of_post_id'],
85                $validated['topic'] ?? null,
86                $validated['insert_role'] ?? null,
87                $validated['prompt_company_new_update_id'] ?? null,
88                $validated['prompt_personal_milestone_id'] ?? null,
89                $validated['persona_id'] ?? null,
90                $validated['prompt_tone_id'] ?? null,
91                $validated['additional_instructions'] ?? null,
92                $validated['custom_prompt_id'] ?? null,
93                $validated['is_regenerate'] ?? false,
94                $dryRun
95            );
96        }
97
98        return response()->json([
99            'status' => 'success',
100            'data' => [
101                'feature' => $feature,
102                'prompt' => $result['prompt'],
103                'response' => $result['response'],
104                'config' => $result['config'] ?? null,
105                'dry_run' => $dryRun,
106            ],
107        ]);
108    }
109}