false, 'error' => 'Profile name is required']); exit; } // Prevent duplicate profile names per user $stmt = $pdo->prepare("SELECT id FROM profiles WHERE user_id = ? AND name = ?"); $stmt->execute([$user_id, $name]); if ($stmt->fetchColumn()) { http_response_code(409); echo json_encode(['success' => false, 'error' => 'Profile already exists']); exit; } // Insert new profile $stmt = $pdo->prepare("INSERT INTO profiles (user_id, name, is_default) VALUES (?, ?, 0)"); $stmt->execute([$user_id, $name]); $newProfileId = $pdo->lastInsertId(); // Make it active immediately $_SESSION['active_profile_id'] = (int)$newProfileId; echo json_encode([ 'success' => true, 'profile_id' => $newProfileId ]);