workspaceId = require_workspace()->id; } public function generate(): void { $this->error = ''; $this->insights = ''; if (! config('services.openai.api_key')) { $this->error = 'OPENAI_API_KEY is not configured. Add it to .env and restart.'; return; } try { $workspace = Workspace::findOrFail($this->workspaceId); $topLinks = Link::where('workspace_id', $workspace->id) ->withCount('clicks') ->orderByDesc('clicks_count') ->limit(10) ->get(['slug', 'target_url', 'clicks_count']); if ($topLinks->isEmpty()) { $this->error = 'No links with clicks found. Create and share some links first.'; return; } $summary = $topLinks->map(fn ($l) => "{$l->slug}: {$l->clicks_count} clicks → {$l->target_url}")->implode("\n"); $prompt = "You are a marketing analyst. Analyze these link performance stats and provide 3-5 actionable insights:\n\n{$summary}\n\nBe concise and specific."; $result = app(AiService::class)->complete($prompt); if ($result === '') { $this->error = 'AI returned an empty response. Check your API key and try again.'; return; } $this->insights = $result; } catch (\Throwable $e) { $this->error = 'Failed to generate insights: ' . $e->getMessage(); } } public function render(): View { return view('livewire.pages.ai.insights') ->layout('layouts.nimuli-app', ['title' => 'AI Insights']); } }