<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode('frontend');

$quoteRepository = $objectManager->get(\Magento\Quote\Api\CartRepositoryInterface::class);

$quoteId = 394135;

echo "Testing QuoteRepository methods for quote ID: " . $quoteId . PHP_EOL;
echo "===========================================" . PHP_EOL;

try {
    // Test get() method
    echo PHP_EOL . "1. Testing get() method:" . PHP_EOL;
    $quote = $quoteRepository->get($quoteId);
    echo "   - Quote found with get()" . PHP_EOL;
    echo "   - Is Active: " . ($quote->getIsActive() ? 'Yes' : 'No') . PHP_EOL;
    echo "   - Items Count: " . $quote->getItemsCount() . PHP_EOL;
    echo "   - Has items: " . ($quote->hasItems() ? 'Yes' : 'No') . PHP_EOL;
    echo "   - Reserved Order ID: " . ($quote->getReservedOrderId() ?: 'None') . PHP_EOL;
} catch (\Exception $e) {
    echo "   - Error: " . $e->getMessage() . PHP_EOL;
}

try {
    // Test getActive() method
    echo PHP_EOL . "2. Testing getActive() method:" . PHP_EOL;
    $activeQuote = $quoteRepository->getActive($quoteId);
    echo "   - Quote found with getActive()" . PHP_EOL;
} catch (\Exception $e) {
    echo "   - Error: " . $e->getMessage() . PHP_EOL;
    echo "   - Exception type: " . get_class($e) . PHP_EOL;
}

// Check if quote has products that might be causing issues
echo PHP_EOL . "3. Checking quote items:" . PHP_EOL;
if (isset($quote)) {
    foreach ($quote->getAllItems() as $item) {
        echo "   - SKU: " . $item->getSku() . PHP_EOL;
        echo "     Product ID: " . $item->getProductId() . PHP_EOL;
        echo "     Qty: " . $item->getQty() . PHP_EOL;
        echo "     Has Error: " . ($item->getHasError() ? 'Yes' : 'No') . PHP_EOL;
        if ($item->getHasError()) {
            echo "     Error Message: " . $item->getMessage() . PHP_EOL;
        }
        
        // Check if product exists
        try {
            $productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
            $product = $productRepository->getById($item->getProductId());
            echo "     Product Status: " . $product->getStatus() . PHP_EOL;
            echo "     Product Type: " . $product->getTypeId() . PHP_EOL;
        } catch (\Exception $e) {
            echo "     Product Error: " . $e->getMessage() . PHP_EOL;
        }
    }
}

// Check for any interceptor issues
echo PHP_EOL . "4. Checking for plugins on QuoteRepository:" . PHP_EOL;
$pluginList = $objectManager->get(\Magento\Framework\Interception\PluginListInterface::class);
$plugins = $pluginList->getNext(\Magento\Quote\Model\QuoteRepository::class, 'getActive');
if ($plugins) {
    echo "   Plugins found on getActive method:" . PHP_EOL;
    foreach ($plugins as $plugin) {
        echo "   - " . get_class($plugin) . PHP_EOL;
    }
} else {
    echo "   No plugins found on getActive method" . PHP_EOL;
}
