isLoggedIn()) { throw new Exception('Unauthorized'); } $db = Database::getInstance(); $action = $_GET['action'] ?? $_POST['action'] ?? ''; switch ($action) { case 'get_impossible_combinations': getImpossibleCombinations($db); break; case 'update_directive_status': updateDirectiveStatus($db); break; default: throw new Exception('Invalid action'); } } catch (Exception $e) { echo json_encode([ 'success' => false, 'error' => $e->getMessage() ]); } function getImpossibleCombinations($db) { $page = (int)($_GET['page'] ?? 1); $limit = (int)($_GET['limit'] ?? 10); $offset = ($page - 1) * $limit; // Get total count $countQuery = $db->query("SELECT COUNT(*) as total FROM panel_directives WHERE llm_checked = 1 AND is_impossible = 1"); $total = $countQuery->fetch_assoc()['total']; // Get paginated results with directive ID generation $query = $db->prepare(" SELECT id, attribute1_name, choice1, attribute2_name, choice2, llm_reasoning, status, updated_at, CONCAT('SYD', LPAD(id, 6, '0')) as directive_id FROM panel_directives WHERE llm_checked = 1 AND is_impossible = 1 ORDER BY updated_at DESC LIMIT ? OFFSET ? "); $query->bind_param('ii', $limit, $offset); $query->execute(); $result = $query->get_result(); $combinations = []; while ($row = $result->fetch_assoc()) { $combinations[] = [ 'id' => $row['id'], 'directive_id' => $row['directive_id'], 'combination' => "{$row['attribute1_name']} = {$row['choice1']} + {$row['attribute2_name']} = {$row['choice2']}", 'reasoning' => $row['llm_reasoning'], 'status' => $row['status'] ?? 'pending', 'updated_at' => $row['updated_at'] ]; } echo json_encode([ 'success' => true, 'combinations' => $combinations, 'pagination' => [ 'page' => $page, 'limit' => $limit, 'total' => (int)$total, 'total_pages' => ceil($total / $limit) ] ]); } function updateDirectiveStatus($db) { $input = json_decode(file_get_contents('php://input'), true); if (!isset($input['id']) || !isset($input['status'])) { throw new Exception('Missing required parameters'); } $id = (int)$input['id']; $status = $input['status']; // Validate status if (!in_array($status, ['pending', 'approved', 'declined'])) { throw new Exception('Invalid status'); } $stmt = $db->prepare("UPDATE panel_directives SET status = ?, updated_at = NOW() WHERE id = ?"); $stmt->bind_param('si', $status, $id); if (!$stmt->execute()) { throw new Exception('Failed to update directive status'); } if ($stmt->affected_rows === 0) { throw new Exception('Directive not found'); } echo json_encode([ 'success' => true, 'message' => 'Directive status updated successfully' ]); } ?>