'Not authenticated']); exit; } $user = getCurrentUser(); if (!$user) { echo json_encode(['error' => 'User not found']); exit; } // Initialize database try { $db = new Database(); $pdo = $db->getConnection(); } catch (Exception $e) { echo json_encode(['error' => 'Database connection failed']); exit; } $action = isset($_GET['action']) ? $_GET['action'] : ''; switch ($action) { case 'status': getProfilerStatus($pdo, $user['id']); break; case 'points': getUserPointsStatus($user['id']); break; case 'completion': getCompletionSummary($pdo, $user['id']); break; default: echo json_encode(['error' => 'Invalid action']); } function getProfilerStatus($pdo, $userId) { $profilerSections = [ 'personal_background' => 'Personal Background', 'household_family' => 'Household & Family', 'shopping_lifestyle' => 'Shopping & Lifestyle', 'technology_digital' => 'Technology & Digital', 'travel_transportation' => 'Travel & Transportation', 'health_fitness' => 'Health & Fitness', 'entertainment_media' => 'Entertainment & Media', 'food_dining' => 'Food & Dining', 'financial_services' => 'Financial Services', 'communication_payments' => 'Communication & Payments' ]; $completion = []; $totalCompleted = 0; try { $stmt = $pdo->prepare("SELECT section, completion_percentage, is_completed, points_awarded FROM profiler_completion WHERE user_id = ?"); $stmt->execute([$userId]); while ($row = $stmt->fetch()) { $completion[$row['section']] = [ 'name' => $profilerSections[$row['section']], 'percentage' => floatval($row['completion_percentage']), 'completed' => boolval($row['is_completed']), 'points_awarded' => boolval($row['points_awarded']) ]; if ($row['is_completed']) { $totalCompleted++; } } // Add sections not started foreach ($profilerSections as $key => $name) { if (!isset($completion[$key])) { $completion[$key] = [ 'name' => $name, 'percentage' => 0, 'completed' => false, 'points_awarded' => false ]; } } // Check mobile verification status $stmt = $pdo->prepare("SELECT is_verified FROM mobile_verifications WHERE user_id = ?"); $stmt->execute([$userId]); $mobileVerified = $stmt->fetch(); echo json_encode([ 'success' => true, 'sections' => $completion, 'total_sections' => count($profilerSections), 'completed_sections' => $totalCompleted, 'overall_percentage' => round(($totalCompleted / count($profilerSections)) * 100, 2), 'mobile_verified' => $mobileVerified ? boolval($mobileVerified['is_verified']) : false ]); } catch (Exception $e) { echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); } } function getUserPointsStatus($userId) { $pm = new PointsManager(); $points = $pm->getUserPoints($userId); $nextTier = $pm->getNextRedemptionTier($points['points']); echo json_encode([ 'success' => true, 'points' => $points, 'next_tier' => $nextTier, 'can_redeem' => $points['points'] >= 200, 'rupee_value' => $points['points'] * 0.5 ]); } function getCompletionSummary($pdo, $userId) { try { // Get overall completion stats $stmt = $pdo->prepare(" SELECT COUNT(*) as total_sections, SUM(CASE WHEN is_completed = 1 THEN 1 ELSE 0 END) as completed_sections, SUM(CASE WHEN points_awarded = 1 THEN 1 ELSE 0 END) as points_awarded_sections FROM profiler_completion WHERE user_id = ? "); $stmt->execute([$userId]); $stats = $stmt->fetch(); if (!$stats) { $stats = ['total_sections' => 0, 'completed_sections' => 0, 'points_awarded_sections' => 0]; } // Get recent transactions $stmt = $pdo->prepare(" SELECT transaction_type, points, source, description, created_at FROM point_transactions WHERE user_id = ? ORDER BY created_at DESC LIMIT 5 "); $stmt->execute([$userId]); $recentTransactions = $stmt->fetchAll(); // Get user points $pm = new PointsManager(); $points = $pm->getUserPoints($userId); echo json_encode([ 'success' => true, 'completion_stats' => $stats, 'points_summary' => $points, 'recent_transactions' => $recentTransactions, 'potential_earnings' => [ 'total_possible' => 70, // 10 sections × 5 points + 10 bonus + 10 mobile 'remaining' => max(0, 70 - $points['total_earned']) ] ]); } catch (Exception $e) { echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); } } ?>