<?php
/**
 * Test Checkout Endpoint - FIXED VERSION
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

// Calculate the correct path using multiple fallbacks
$base_path = null;

// Method 1: Try dirname(__DIR__, 3) - should go up three levels to root
$base_path = dirname(__DIR__, 3);

// Method 2: If config doesn't exist there, try manual calculation
if (!file_exists($base_path . '/config/config.php')) {
    // Manual calculation: /HMSPOS/pos/sales/ -> /HMSPOS/
    $base_path = dirname(dirname(dirname(__DIR__)));
}

// Method 3: If still not found, use absolute path as last resort
if (!file_exists($base_path . '/config/config.php')) {
    $base_path = '/Applications/XAMPP/xamppfiles/htdocs/HMSPOS';
}

// Load config
require_once $base_path . '/config/config.php';

echo "<h1>Checkout Test</h1>";
echo "<p>Base path: " . $base_path . "</p>";

// Check if checkout.php exists
$checkout_file = __DIR__ . '/checkout.php';
if (file_exists($checkout_file)) {
    echo "<p style='color:green'>✅ checkout.php exists at: $checkout_file</p>";
    echo "<p>File permissions: " . substr(sprintf('%o', fileperms($checkout_file)), -4) . "</p>";
} else {
    echo "<p style='color:red'>❌ checkout.php not found at: $checkout_file</p>";
}

// Check database tables
try {
    require_once $base_path . '/config/database.php';
    $pdo = get_db_connection();
    
    $tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
    echo "<h2>Database Tables:</h2>";
    echo "<pre>" . print_r($tables, true) . "</pre>";
    
    if (in_array('pos_sales', $tables)) {
        echo "<p style='color:green'>✅ pos_sales table exists</p>";
    } else {
        echo "<p style='color:red'>❌ pos_sales table does not exist</p>";
    }
    
    if (in_array('pos_sale_items', $tables)) {
        echo "<p style='color:green'>✅ pos_sale_items table exists</p>";
    } else {
        echo "<p style='color:red'>❌ pos_sale_items table does not exist</p>";
    }
    
    if (in_array('pos_shifts', $tables)) {
        echo "<p style='color:green'>✅ pos_shifts table exists</p>";
    } else {
        echo "<p style='color:red'>❌ pos_shifts table does not exist</p>";
    }
    
} catch (Exception $e) {
    echo "<p style='color:red'>Database error: " . $e->getMessage() . "</p>";
}

echo "<hr>";
echo "<p><a href='pos.php'>Back to POS Terminal</a></p>";
?>