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

$connection = $objectManager->get(\Magento\Framework\App\ResourceConnection::class)->getConnection();

// Check the COD orders from quote 394135
$sql = "SELECT 
    o.entity_id,
    o.increment_id,
    o.state,
    o.status,
    o.total_paid,
    o.grand_total,
    p.method as payment_method,
    o.created_at
FROM sales_order o
JOIN sales_order_payment p ON p.parent_id = o.entity_id
WHERE o.quote_id = 394135
ORDER BY o.created_at DESC";

$orders = $connection->fetchAll($sql);

echo "COD Orders from Quote 394135:" . PHP_EOL;
echo "=============================" . PHP_EOL;

foreach ($orders as $order) {
    echo PHP_EOL . "Order #" . $order['increment_id'] . PHP_EOL;
    echo "- Payment Method: " . $order['payment_method'] . PHP_EOL;
    echo "- Total Paid: " . $order['total_paid'] . PHP_EOL;
    echo "- Grand Total: " . $order['grand_total'] . PHP_EOL;
    echo "- Status: " . $order['status'] . PHP_EOL;
    echo "- Condition check: " . PHP_EOL;
    echo "  - TotalPaid < GrandTotal: " . (($order['total_paid'] < $order['grand_total']) ? 'TRUE' : 'FALSE') . PHP_EOL;
    echo "  - Status == 'custom_neplatit': " . (($order['status'] == 'custom_neplatit') ? 'TRUE' : 'FALSE') . PHP_EOL;
    echo "  - Would reactivate quote: " . (($order['total_paid'] < $order['grand_total'] || $order['status'] == 'custom_neplatit') ? 'YES' : 'NO') . PHP_EOL;
}

// Check if OscIndex plugin is running
echo PHP_EOL . "Checking for OscIndex plugin presence:" . PHP_EOL;
$moduleManager = $objectManager->get(\Magento\Framework\Module\Manager::class);
if ($moduleManager->isEnabled('Leadlion_Checkout')) {
    echo "- Leadlion_Checkout module is ENABLED" . PHP_EOL;
    
    // Check if plugin is registered
    $pluginList = $objectManager->get(\Magento\Framework\Interception\PluginListInterface::class);
    $plugins = $pluginList->getNext('Mageplaza\Osc\Controller\Index\Index', 'execute');
    if ($plugins) {
        echo "- OscIndex plugin is ACTIVE" . PHP_EOL;
    }
} else {
    echo "- Leadlion_Checkout module is DISABLED" . PHP_EOL;
}
