namespace App\Http\Controllers\Web; use App\Http\Controllers\Controller; use App\Models\Order; use App\Models\Transaction; use App\Models\Ticket; use App\Models\Referral; use App\Services\Analytics\DashboardAnalytics; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class DashboardController extends Controller { protected $analytics; public function __construct(DashboardAnalytics $analytics) { $this->analytics = $analytics; } // Main dashboard public function index() { $user = auth()->user(); $data = [ // Wallet Overview 'wallet_balance' => $user->wallet?->balance ?? 0, 'pending_balance' => $user->wallet?->pending_balance ?? 0, 'total_deposited' => $user->total_deposited, 'total_spent' => $user->total_spent, // Order Stats 'total_orders' => $user->orders()->count(), 'completed_orders' => $user->orders()->where('status', 'completed')->count(), 'pending_orders' => $user->orders()->whereIn('status', ['pending', 'processing'])->count(), 'recent_orders' => $user->orders()->latest()->limit(5)->get(), // Transaction Stats 'recent_transactions' => $user->transactions()->latest()->limit(5)->get(), 'monthly_spending' => $user->monthly_spending, 'today_spending' => $user->today_spending, // Referral Stats 'referral_count' => $user->referral_count, 'referral_earnings' => $user->referral_earnings, 'recent_referrals' => $user->referrals()->with('referred')->latest()->limit(5)->get(), // Ticket Stats 'open_tickets' => $user->tickets()->where('status', 'open')->count(), 'recent_tickets' => $user->tickets()->latest()->limit(3)->get(), // Charts Data 'spending_chart' => $this->getSpendingChartData($user), 'orders_chart' => $this->getOrdersChartData($user), // Analytics 'popular_services' => $this->getPopularServices($user), 'activity_stats' => $user->activity_stats, ]; return view('web.dashboard.index', $data); } // Profile page public function profile() { $user = auth()->user(); return view('web.dashboard.profile', compact('user')); } // Update profile public function updateProfile(Request $request) { $user = auth()->user(); $request->validate([ 'first_name' => 'required|string|max:50', 'last_name' => 'required|string|max:50', 'phone' => 'nullable|string|max:20', 'country' => 'nullable|string|max:50', 'city' => 'nullable|string|max:50', 'address' => 'nullable|string|max:500', ]); $oldData = $user->only(['first_name', 'last_name', 'phone', 'country', 'city', 'address']); $user->update($request->only([ 'first_name', 'last_name', 'phone', 'country', 'city', 'address' ])); $user->logActivity('Profile updated', [ 'old' => $oldData, 'new' => $request->only(['first_name', 'last_name', 'phone', 'country', 'city', 'address']) ], 'profile_update'); return back()->with('success', 'Profile updated successfully.'); } // Security settings public function security() { $user = auth()->user(); $recentLogins = $user->loginLogs()->latest()->limit(10)->get(); return view('web.dashboard.security', compact('user', 'recentLogins')); } // Enable 2FA public function enableTwoFactor(Request $request) { $user = auth()->user(); if ($user->two_factor_enabled) { return back()->with('error', '2FA is already enabled.'); } $secret = $this->twoFactorService->generateSecret(); $user->two_factor_secret = encrypt($secret); $user->save(); $qrCode = $this->twoFactorService->getQRCode($user, $secret); return view('web.dashboard.two-factor-enable', [ 'secret' => $secret, 'qrCode' => $qrCode, ]); } // Confirm 2FA public function confirmTwoFactor(Request $request) { $request->validate([ 'code' => 'required|string', ]); $user = auth()->user(); if ($this->twoFactorService->verify($user, $request->code)) { $user->two_factor_enabled = true; $user->save(); $user->logActivity('2FA enabled', [], 'security'); return redirect()->route('dashboard.security') ->with('success', 'Two-factor authentication enabled successfully.'); } return back()->withErrors(['code' => 'Invalid verification code.']); } // Disable 2FA public function disableTwoFactor(Request $request) { $request->validate([ 'password' => 'required|current_password', ]); $user = auth()->user(); $user->two_factor_enabled = false; $user->two_factor_secret = null; $user->save(); $user->logActivity('2FA disabled', [], 'security'); return back()->with('success', 'Two-factor authentication disabled.'); } // Activity log public function activity() { $user = auth()->user(); $activities = $user->activityLogs()->with('subject')->latest()->paginate(20); return view('web.dashboard.activity', compact('activities')); } // Notifications public function notifications() { $user = auth()->user(); $notifications = $user->notifications()->latest()->paginate(20); return view('web.dashboard.notifications', compact('notifications')); } // Mark notification as read public function markNotificationRead($id) { $user = auth()->user(); $notification = $user->notifications()->findOrFail($id); $notification->markAsRead(); return response()->json(['success' => true]); } // Mark all notifications as read public function markAllNotificationsRead() { $user = auth()->user(); $user->unreadNotifications->markAsRead(); return back()->with('success', 'All notifications marked as read.'); } // Helper methods for charts protected function getSpendingChartData($user) { $data = []; $labels = []; for ($i = 6; $i >= 0; $i--) { $date = now()->subDays($i); $labels[] = $date->format('M d'); $spending = $user->transactions() ->where('type', 'order') ->where('status', 'completed') ->whereDate('created_at', $date) ->sum('amount'); $data[] = $spending; } return [ 'labels' => $labels, 'data' => $data, ]; } protected function getOrdersChartData($user) { $statuses = ['pending', 'processing', 'completed', 'cancelled']; $data = []; foreach ($statuses as $status) { $count = $user->orders()->where('status', $status)->count(); $data[$status] = $count; } return $data; } protected function getPopularServices($user) { return $user->orders() ->select('service_id', DB::raw('count(*) as total')) ->with('service') ->groupBy('service_id') ->orderByDesc('total') ->limit(5) ->get(); } }