orderBy('name')->get(); return view('settings.notifications', compact('settings')); } public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'name' => 'required|string|max:100', 'channel' => 'required|in:email,telegram,whatsapp', 'triggers' => 'nullable|array', 'triggers.*' => 'in:upcoming_event,invoice_due,content_due', 'notify_before_hours' => 'nullable|integer|min:1|max:720', // Email 'config.to_email' => 'required_if:channel,email|nullable|email', // Telegram 'config.bot_token' => 'required_if:channel,telegram|nullable|string', 'config.chat_id' => 'required_if:channel,telegram|nullable|string', // WhatsApp (WAHA) 'config.waha_url' => 'required_if:channel,whatsapp|nullable|url', 'config.waha_session' => 'required_if:channel,whatsapp|nullable|string', 'config.phone' => 'required_if:channel,whatsapp|nullable|string', ]); NotificationSetting::create([ 'name' => $validated['name'], 'channel' => $validated['channel'], 'config' => $validated['config'] ?? [], 'triggers' => $validated['triggers'] ?? [], 'notify_before_hours' => $validated['notify_before_hours'] ?? 24, 'is_enabled' => true, ]); return back()->with('success', '🔔 Notification channel added!'); } public function update(Request $request, NotificationSetting $setting): RedirectResponse { $validated = $request->validate([ 'name' => 'required|string|max:100', 'triggers' => 'nullable|array', 'triggers.*' => 'in:upcoming_event,invoice_due,content_due', 'notify_before_hours' => 'nullable|integer|min:1|max:720', 'config' => 'nullable|array', ]); $setting->update([ 'name' => $validated['name'], 'triggers' => $validated['triggers'] ?? [], 'notify_before_hours' => $validated['notify_before_hours'] ?? $setting->notify_before_hours, 'config' => $validated['config'] ?? $setting->config, ]); return back()->with('success', '🔔 Notification updated!'); } public function destroy(NotificationSetting $setting): RedirectResponse { $setting->delete(); return back()->with('success', '🔔 Channel removed.'); } public function toggle(NotificationSetting $setting): RedirectResponse { $setting->update(['is_enabled' => ! $setting->is_enabled]); return back()->with('success', $setting->is_enabled ? '🔔 Enabled!' : '🔕 Disabled.'); } public function test(NotificationSetting $setting): RedirectResponse { try { app(NotificationDispatcher::class)->sendTest($setting); return back()->with('success', '✅ Test notification sent!'); } catch (\Throwable $e) { return back()->with('error', '❌ Failed: ' . $e->getMessage()); } } }