<!doctype html>
<html>
<head>
    <title>Database Manager - HUGE</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="data:;base64,=">
    <link rel="stylesheet" href="<?php echo Config::get('URL'); ?>css/style.css" />
    <link rel="stylesheet" href="<?php echo Config::get('URL'); ?>css/dbmanager.css" />
    <script src="https://unpkg.com/lucide@latest"></script>
</head>
<body>
    <div class="wrapper dbm-page-wrapper">
        <?php
        $uri = trim($_SERVER['REQUEST_URI'], '/');
        $uri_parts = explode('/', $uri);
        $current_controller = isset($uri_parts[0]) ? strtolower($uri_parts[0]) : 'database';
        $is_db_page = in_array($current_controller, ['database', 'table', 'sql']);
        $is_user_page = ($current_controller === 'dbuser');
        ?>
        <ul class="navigation">
            <li><a href="<?php echo Config::get('URL'); ?>index/index">Home</a></li>
            <li class="<?php echo $is_db_page ? 'active' : ''; ?>"><a href="<?php echo Config::get('URL'); ?>database/index">Database</a></li>
            <li class="<?php echo $is_user_page ? 'active' : ''; ?>"><a href="<?php echo Config::get('URL'); ?>dbuser/index">Users</a></li>
        </ul>

        <ul class="navigation right">
            <li><a href="<?php echo Config::get('URL'); ?>admin/">Admin</a></li>
            <li><a href="<?php echo Config::get('URL'); ?>login/logout">Logout</a></li>
        </ul>

        <div class="dbm-wrapper">
            <div class="dbm-main">
                <aside class="dbm-sidebar">
                    <div class="dbm-sidebar-header">
                        <i data-lucide="database" class="icon"></i>
                        <h3>Databases</h3>
                    </div>

                    <nav class="dbm-tree">
                        <?php
                        $all_databases = DatabaseModel::getAllDatabases();
                        $current_database = isset($this->database_name) ? $this->database_name : Config::get('DB_NAME');
                        $current_table = isset($this->table_name) ? $this->table_name : null;

                        foreach ($all_databases as $db):
                            $is_current_db = ($db === $current_database);
                            $tables = $is_current_db ? DatabaseModel::getTablesInDatabase($db) : [];
                        ?>
                            <div class="tree-item <?php echo $is_current_db ? 'expanded' : ''; ?>" data-db="<?php echo htmlspecialchars($db); ?>">
                                <div class="tree-header <?php echo $is_current_db ? 'active' : ''; ?>" data-href="<?php echo Config::get('URL'); ?>database/show/<?php echo urlencode($db); ?>">
                                    <span class="tree-toggle">
                                        <i data-lucide="chevron-right"></i>
                                    </span>
                                    <span class="tree-icon database">
                                        <i data-lucide="database"></i>
                                    </span>
                                    <span class="tree-label"><?php echo htmlspecialchars($db); ?></span>
                                    <?php if (!empty($tables)): ?>
                                        <span class="tree-badge"><?php echo count($tables); ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="tree-children" <?php echo $is_current_db ? 'data-loaded="true"' : ''; ?>>
                                    <?php if ($is_current_db && !empty($tables)): ?>
                                        <?php foreach ($tables as $table):
                                            $is_current_table = ($table === $current_table);
                                            $columns = $is_current_table ? TableModel::getTableColumns($db, $table) : [];
                                        ?>
                                            <div class="tree-item <?php echo $is_current_table ? 'expanded' : ''; ?>" data-table="<?php echo htmlspecialchars($table); ?>">
                                                <div class="tree-header <?php echo $is_current_table ? 'active' : ''; ?>" data-href="<?php echo Config::get('URL'); ?>table/show/<?php echo urlencode($db); ?>/<?php echo urlencode($table); ?>">
                                                    <span class="tree-toggle">
                                                        <i data-lucide="chevron-right"></i>
                                                    </span>
                                                    <span class="tree-icon table">
                                                        <i data-lucide="table"></i>
                                                    </span>
                                                    <span class="tree-label"><?php echo htmlspecialchars($table); ?></span>
                                                </div>
                                                <div class="tree-children"<?php echo $is_current_table ? ' data-loaded="true"' : ''; ?>>
                                                    <?php if ($is_current_table && !empty($columns)): ?>
                                                        <?php foreach ($columns as $col): ?>
                                                            <div class="tree-item">
                                                                <div class="tree-header">
                                                                    <span class="tree-icon <?php echo $col['Key'] === 'PRI' ? 'key' : 'column'; ?>">
                                                                        <?php if ($col['Key'] === 'PRI'): ?>
                                                                            <i data-lucide="key-round"></i>
                                                                        <?php else: ?>
                                                                            <i data-lucide="columns-2"></i>
                                                                        <?php endif; ?>
                                                                    </span>
                                                                    <span class="tree-label"><?php echo htmlspecialchars($col['Field']); ?></span>
                                                                    <span class="tree-badge"><?php echo htmlspecialchars($col['Type']); ?></span>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </nav>
                </aside>

                <main class="dbm-content">
