<?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');

// Simulate the exact flow from GuestPaymentInformationManagement
$quoteIdMaskFactory = $objectManager->get(\Magento\Quote\Model\QuoteIdMaskFactory::class);
$cartRepository = $objectManager->get(\Magento\Quote\Api\CartRepositoryInterface::class);

$maskedCartId = 'Na6CiXgKZBzM1Ixl4MMQbyNsVfGuuBnC';

echo "Simulating GuestPaymentInformationManagement flow:" . PHP_EOL;
echo "================================================" . PHP_EOL;

try {
    // This is exactly what happens in GuestPaymentInformationManagement line 179-181
    echo "1. Loading quote mask..." . PHP_EOL;
    $quoteIdMask = $quoteIdMaskFactory->create()->load($maskedCartId, 'masked_id');
    
    echo "   - Mask ID: " . $quoteIdMask->getId() . PHP_EOL;
    echo "   - Quote ID: " . $quoteIdMask->getQuoteId() . PHP_EOL;
    echo "   - Masked ID: " . $quoteIdMask->getMaskedId() . PHP_EOL;
    
    echo PHP_EOL . "2. Getting active quote..." . PHP_EOL;
    $quote = $cartRepository->getActive($quoteIdMask->getQuoteId());
    
    echo "   - SUCCESS: Quote retrieved" . PHP_EOL;
    echo "   - Quote ID: " . $quote->getId() . PHP_EOL;
    echo "   - Is Active: " . ($quote->getIsActive() ? 'Yes' : 'No') . PHP_EOL;
    
} catch (\Exception $e) {
    echo "   - ERROR: " . $e->getMessage() . PHP_EOL;
    echo "   - Exception: " . get_class($e) . PHP_EOL;
    echo "   - Code: " . $e->getCode() . PHP_EOL;
    
    // Try to get more details
    if ($quoteIdMask && $quoteIdMask->getQuoteId()) {
        echo PHP_EOL . "3. Debugging quote directly:" . PHP_EOL;
        try {
            $quote = $cartRepository->get($quoteIdMask->getQuoteId());
            echo "   - Quote exists with ID: " . $quote->getId() . PHP_EOL;
            echo "   - Is Active: " . ($quote->getIsActive() ? 'Yes' : 'No') . PHP_EOL;
            echo "   - Store ID: " . $quote->getStoreId() . PHP_EOL;
            echo "   - Customer Group: " . $quote->getCustomerGroupId() . PHP_EOL;
            
            // Check if it's a timing issue
            $connection = $objectManager->get(\Magento\Framework\App\ResourceConnection::class)->getConnection();
            $sql = "SELECT is_active, updated_at FROM quote WHERE entity_id = :quote_id";
            $result = $connection->fetchRow($sql, ['quote_id' => $quote->getId()]);
            echo "   - DB is_active: " . $result['is_active'] . PHP_EOL;
            echo "   - DB updated_at: " . $result['updated_at'] . PHP_EOL;
            
        } catch (\Exception $e2) {
            echo "   - Quote not found: " . $e2->getMessage() . PHP_EOL;
        }
    }
}

// Check if this is a store-specific issue
echo PHP_EOL . "4. Checking store context:" . PHP_EOL;
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
echo "   - Current Store ID: " . $storeManager->getStore()->getId() . PHP_EOL;
echo "   - Current Store Code: " . $storeManager->getStore()->getCode() . PHP_EOL;
