isLoggedIn()) { throw new Exception('Unauthorized'); } $type = $_GET['type'] ?? 'possible'; $limit = isset($_GET['limit']) ? (int)$_GET['limit'] : null; if (!in_array($type, ['possible', 'impossible'])) { throw new Exception('Invalid type. Must be "possible" or "impossible"'); } $db = Database::getInstance(); $combinations = getCombinations($db, $type, $limit); echo json_encode([ 'success' => true, 'type' => $type, 'count' => count($combinations), 'combinations' => $combinations ]); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); } function getCombinations($db, $type, $limit) { try { $isImpossible = $type === 'impossible' ? 1 : 0; $sql = "SELECT id, attribute1_id, attribute2_id, choice1, choice2, attribute1_name, attribute2_name, is_impossible, llm_reasoning, updated_at FROM panel_directives WHERE llm_checked = 1 AND is_impossible = ? ORDER BY updated_at DESC"; if ($limit) { $sql .= " LIMIT ?"; } $stmt = $db->prepare($sql); if ($limit) { $stmt->bind_param('ii', $isImpossible, $limit); } else { $stmt->bind_param('i', $isImpossible); } $stmt->execute(); $result = $stmt->get_result(); $combinations = []; while ($row = $result->fetch_assoc()) { $combinations[] = [ 'id' => $row['id'], 'attribute1_id' => $row['attribute1_id'], 'attribute2_id' => $row['attribute2_id'], 'choice1' => $row['choice1'], 'choice2' => $row['choice2'], 'attribute1_name' => $row['attribute1_name'], 'attribute2_name' => $row['attribute2_name'], 'is_impossible' => (bool)$row['is_impossible'], 'reasoning' => $row['llm_reasoning'], 'updated_at' => $row['updated_at'], 'formatted_time' => formatTimeAgo($row['updated_at']) ]; } return $combinations; } catch (Exception $e) { error_log("Error getting combinations: " . $e->getMessage()); return []; } } function formatTimeAgo($datetime) { $time = time() - strtotime($datetime); if ($time < 60) { return 'Just now'; } elseif ($time < 3600) { $minutes = floor($time / 60); return $minutes . ' minute' . ($minutes > 1 ? 's' : '') . ' ago'; } elseif ($time < 86400) { $hours = floor($time / 3600); return $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago'; } else { $days = floor($time / 86400); return $days . ' day' . ($days > 1 ? 's' : '') . ' ago'; } } ?>