<?php

// Path to your database.db
$dbFile = __DIR__ . '/database.db';

try {
    // Connect using PDO
    $db = new PDO('sqlite:' . $dbFile);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

    echo "<!DOCTYPE html>";
    echo "<html>";
    echo "<head>";
    echo "<meta charset='UTF-8'>";
    echo "<title>Database Structure</title>";

    echo "<style>
        body {
            font-family: Arial, sans-serif;
            background: #f5f5f5;
            padding: 30px;
        }

        h1 {
            margin-bottom: 5px;
        }

        .database {
            color: #555;
            margin-bottom: 30px;
        }

        .table-box {
            background: white;
            padding: 20px;
            margin-bottom: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        }

        h2 {
            margin-top: 0;
            color: #222;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }

        th {
            background: #eee;
        }

        .primary {
            color: green;
            font-weight: bold;
        }

        .empty {
            color: #999;
        }

        code {
            background: #eee;
            padding: 2px 5px;
            border-radius: 3px;
        }
    </style>";

    echo "</head>";
    echo "<body>";

    echo "<h1>SQLite Database Structure</h1>";
    echo "<div class='database'>Database: <strong>" . htmlspecialchars(basename($dbFile)) . "</strong></div>";

    // Get all tables
    $tables = $db->query("
        SELECT name
        FROM sqlite_master
        WHERE type = 'table'
        AND name NOT LIKE 'sqlite_%'
        ORDER BY name
    ")->fetchAll();

    if (!$tables) {
        echo "<p class='empty'>No tables found in the database.</p>";
    }

    foreach ($tables as $table) {

        $tableName = $table['name'];

        echo "<div class='table-box'>";
        echo "<h2>Table: <code>" . htmlspecialchars($tableName) . "</code></h2>";

        // Get table structure
        $stmt = $db->query(
            "PRAGMA table_info(" . $db->quote($tableName) . ")"
        );

        $columns = $stmt->fetchAll();

        if (!$columns) {
            echo "<p class='empty'>No columns found.</p>";
            echo "</div>";
            continue;
        }

        echo "<table>";

        echo "<tr>";
        echo "<th>#</th>";
        echo "<th>Column Name</th>";
        echo "<th>Data Type</th>";
        echo "<th>NOT NULL</th>";
        echo "<th>Default Value</th>";
        echo "<th>Primary Key</th>";
        echo "</tr>";

        foreach ($columns as $column) {

            echo "<tr>";

            echo "<td>" . htmlspecialchars($column['cid']) . "</td>";

            echo "<td><strong>" .
                htmlspecialchars($column['name']) .
                "</strong></td>";

            echo "<td>" .
                htmlspecialchars($column['type'] ?: 'NONE') .
                "</td>";

            echo "<td>" .
                ($column['notnull'] ? 'YES' : 'NO') .
                "</td>";

            echo "<td>";

            if ($column['dflt_value'] === null) {
                echo "<span class='empty'>NULL</span>";
            } else {
                echo htmlspecialchars($column['dflt_value']);
            }

            echo "</td>";

            echo "<td>";

            if ($column['pk']) {
                echo "<span class='primary'>YES</span>";
            } else {
                echo "NO";
            }

            echo "</td>";

            echo "</tr>";
        }

        echo "</table>";

        echo "</div>";
    }

    echo "</body>";
    echo "</html>";
} catch (PDOException $e) {

    echo "<h2>Database Error</h2>";
    echo "<p>" . htmlspecialchars($e->getMessage()) . "</p>";
}
#0b1a2e;