isLoggedIn()) { throw new Exception('Unauthorized'); } $page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1; $limit = isset($_GET['limit']) ? max(1, min(100, (int)$_GET['limit'])) : 10; $offset = ($page - 1) * $limit; $db = Database::getInstance(); // Get total count of impossible combinations $countQuery = $db->query(" SELECT COUNT(*) as total FROM panel_directives WHERE llm_checked = 1 AND is_impossible = 1 "); $totalCount = $countQuery ? $countQuery->fetch_assoc()['total'] : 0; $totalPages = ceil($totalCount / $limit); // Get paginated impossible combinations $stmt = $db->prepare(" SELECT id, attribute1_id, attribute2_id, choice1, choice2, attribute1_name, attribute2_name, status, llm_reasoning, updated_at FROM panel_directives WHERE llm_checked = 1 AND is_impossible = 1 ORDER BY updated_at DESC LIMIT ? OFFSET ? "); $stmt->bind_param('ii', $limit, $offset); $stmt->execute(); $result = $stmt->get_result(); $combinations = []; while ($row = $result->fetch_assoc()) { $combinations[] = [ 'id' => (int)$row['id'], 'attribute1_id' => (int)$row['attribute1_id'], 'attribute2_id' => (int)$row['attribute2_id'], 'choice1' => $row['choice1'], 'choice2' => $row['choice2'], 'attribute1_name' => $row['attribute1_name'], 'attribute2_name' => $row['attribute2_name'], 'status' => $row['status'], 'reasoning' => $row['llm_reasoning'], 'updated_at' => $row['updated_at'] ]; } echo json_encode([ 'success' => true, 'combinations' => $combinations, 'pagination' => [ 'page' => $page, 'limit' => $limit, 'total' => (int)$totalCount, 'totalPages' => (int)$totalPages, 'hasNext' => $page < $totalPages, 'hasPrev' => $page > 1 ], // Legacy fields for compatibility 'page' => $page, 'total' => (int)$totalCount, 'totalPages' => (int)$totalPages ]); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'message' => $e->getMessage(), 'combinations' => [], 'pagination' => [ 'page' => 1, 'limit' => 10, 'total' => 0, 'totalPages' => 0, 'hasNext' => false, 'hasPrev' => false ] ]); }