<?php
require_once '../includes/config.php';
session_start();

header('Content-Type: application/json');

$response = [
    'session' => $_SESSION,
    'session_id' => session_id(),
    'db_connection' => (isset($conn) && $conn) ? 'connected' : 'failed',
    'db_name' => defined('DB_NAME') ? DB_NAME : 'undefined',
    'tables' => []
];

if (isset($conn) && $conn) {
    if ($conn->select_db(DB_NAME)) {
        $result = $conn->query("SHOW TABLES");
        while ($row = $result->fetch_row()) {
            $response['tables'][] = $row[0];
            if ($row[0] === 'cart_items') {
                $cols = $conn->query("DESCRIBE cart_items");
                $response['cart_items_schema'] = [];
                while ($c = $cols->fetch_assoc()) {
                    $response['cart_items_schema'][] = $c;
                }
            }
        }
    } else {
        $response['db_error'] = $conn->error;
    }
}

echo json_encode($response);
?>
