<?php

declare(strict_types=1);

$pageStyles = [];
$pageScripts = [];

require_once __DIR__ . '/db.php';
require_once __DIR__ . '/auth.php';

const THEME_STYLES = [
    'white' => 'app/css/white_mode.css',
    'dark' => 'app/css/dark_mode.css',
    'purple' => 'app/css/purple_mode.css',
    'green' => 'app/css/green_mode.css',
    'beige' => 'app/css/beige_mode.css',
];

function projectKilnTableExists(string $table): bool
{
    $stmt = db()->prepare(
        'SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?'
    );
    $stmt->execute([$table]);

    return (int)$stmt->fetchColumn() > 0;
}

function projectKilnIsInstalled(): bool
{
    if (!projectKilnTableExists('settings')) {
        return false;
    }

    $stmt = db()->prepare(
        "SELECT COUNT(*) FROM settings WHERE setting_name = 'installed' AND LOWER(setting_value) = 'true'"
    );
    $stmt->execute();

    return (int)$stmt->fetchColumn() > 0;
}

function currentTheme(): string
{
    $theme = 'dark';
    $user = currentUser();

    if (!$user) {
        return $theme;
    }

    $stmt = db()->prepare(
        'SELECT setting_value
         FROM user_settings
         WHERE user_id = ?
           AND setting_name = ?
         LIMIT 1'
    );

    $stmt->execute([
        (int)$user['id'],
        'theme'
    ]);

    $storedTheme = (string)($stmt->fetchColumn() ?: '');

    return array_key_exists($storedTheme, THEME_STYLES) ? $storedTheme : $theme;
}

$isInstalled = projectKilnIsInstalled();
$theme = $isInstalled ? currentTheme() : 'dark';

if (!$isInstalled) {
    define('PROJECTKILN_INSTALL_EMBEDDED', true);

    ob_start();
    require __DIR__ . '/install/install.php';
    $pageContent = ob_get_clean();
} else {
    $page = $_GET['page'] ?? 'home';

    $routes = [
        'home'     => 'home.php',
        'login'    => 'login.php',
        'logout'    => 'logout.php',
    ];

    if (!isset($routes[$page])) {
        $pageFile = __DIR__ . '/app/404.php';
    } else {
        $pageFile = __DIR__ . '/app/' . $routes[$page];
    }

    ob_start();
    require $pageFile;
    $pageContent = ob_get_clean();
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>ProjectKiln</title>

        <link rel="stylesheet" href="app/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="app/fontawesome/css/all.min.css">

        <link rel="stylesheet" href="app/css/main.css">
        <link
            rel="stylesheet"
            href="<?= htmlspecialchars(THEME_STYLES[$theme]) ?>"
            id="themeStylesheet"
            data-theme="<?= htmlspecialchars($theme) ?>"
        >

        <?php foreach ($pageStyles as $style): ?>
            <link rel="stylesheet" href="<?= htmlspecialchars($style) ?>">
        <?php endforeach; ?>
    </head>
    <body>
        <main>
            <?= $pageContent ?>
        </main>

        <script src="app/bootstrap/js/bootstrap.bundle.min.js"></script>

        <script src="app/js/main.js"></script>
        
        <?php foreach ($pageScripts as $script): ?>
            <script src="<?= htmlspecialchars($script) ?>"></script>
        <?php endforeach; ?>
    </body>
</html>
