<?php

declare(strict_types=1);
// Variables provided by TablesController: $baseUrl, $assetsUrl, $project, $user, $navItems, $tablesJson, $pageTitle, $savedTablesJson, $savedOrderJson
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title><?= htmlspecialchars($project['name'] ?? 'Project') ?> - Tables</title>

    <?php include constant('BASE_PATH') . '/core/views/project-head.php'; ?>

    <?= \PpPhp\Core\ModuleAssets::style('Tables', 'Views/styles.css') ?>

    <?php
    // Bundled CodeMirror for the SQL Editor. Cache-bust by mtime+size so an edit to
    // EITHER bundle (js or css) forces a fresh download (the URLs are otherwise
    // un-versioned). Fold both files in so editing only the CSS still busts it.
    $cmVer = '';
    foreach (['js', 'css'] as $__ext) {
        foreach ([
            constant('BASE_PATH') . "/public/assets/codemirror/codemirror.bundle.$__ext",
            constant('BASE_PATH') . "/assets/codemirror/codemirror.bundle.$__ext",
        ] as $__cm) {
            if (is_file($__cm)) { $cmVer .= filemtime($__cm) . '-' . filesize($__cm) . '.'; break; }
        }
    }
    $cmVer = $cmVer !== '' ? rtrim($cmVer, '.') : '1';
    ?>
    <link href="<?= htmlspecialchars($assetsUrl ?? '') ?>/codemirror/codemirror.bundle.css?v=<?= htmlspecialchars($cmVer) ?>" rel="stylesheet">
    <script src="<?= htmlspecialchars($assetsUrl ?? '') ?>/codemirror/codemirror.bundle.js?v=<?= htmlspecialchars($cmVer) ?>"></script>
</head>

<body class="tables-module">

    <?php include __DIR__ . '/partials/shell.php'; ?>

    <div id="mainArea" class="main-area">
        <?php include __DIR__ . '/partials/table_panel.php'; ?>
        <?php include __DIR__ . '/partials/workspace.php'; ?>
    </div>

    <?php include __DIR__ . '/partials/create_table_modal.php'; ?>
    <?php include __DIR__ . '/partials/edit_table_modal.php'; ?>
    <?php include __DIR__ . '/partials/create_custom_view_modal.php'; ?>
    <?php include __DIR__ . '/partials/create_sql_view_modal.php'; ?>
    <?php include __DIR__ . '/partials/sql_editor_modal.php'; ?>
    <?php include __DIR__ . '/partials/create_calendar_modal.php'; ?>
    <?php include __DIR__ . '/partials/relation_modal.php'; ?>
    <?php include __DIR__ . '/partials/relation_edit_modal.php'; ?>
    <?php include __DIR__ . '/partials/help.php'; ?>

    <?= \PpPhp\Core\ModuleAssets::style('Tables', 'Views/tablesTheme.css') ?>



    <?php include constant('BASE_PATH') . '/core/views/project-scripts.php'; ?>

    <!-- PHP data passed to JavaScript -->
    <script>
        const projectSlug = <?= json_encode($project['slug'] ?? '') ?>;
        const baseUrl = <?= json_encode($baseUrl) ?>;
        allTables = <?= json_encode($tables) ?>;
        savedTables = <?= json_encode($savedTables) ?>;
        savedPositions = <?= json_encode($savedPositions) ?>;
        savedExpandedTables = <?= json_encode($expandedTables) ?>;
        savedTableListSort = <?= json_encode($tableListSort) ?>;
        savedTableListFilter = <?= json_encode($tableListFilter) ?>;
        window.databaseDialect = <?= json_encode($databaseDialect, JSON_UNESCAPED_SLASHES) ?>;
        // Note: no 'let' or 'const' for shared globals
    </script>

    <!-- Modular scripts (order matters) -->
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/core.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/tableList.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/cards.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/editTable.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/relations.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/save.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/sync.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/calendar.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/init.js') ?>
    <?= \PpPhp\Core\ModuleAssets::script('Tables', 'Views/scripts/sqlEditor.js') ?>



</body>

</html>
