
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

/*
 |---------------------------------------------------------
 | Hoonhub upgrade.php
 | Run once to upgrade site structure + theme.
 | Then DELETE this file.
 |---------------------------------------------------------
*/

function writeFile($path, $content) {
    $dir = dirname($path);
    if (!is_dir($dir)) {
        mkdir($dir, 0755, true);
    }
    file_put_contents($path, $content);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // ===============================
    // CSS THEME
    // ===============================
    writeFile('assets/css/style.css', <<<'CSS'
:root {
    --bg:#0f1115;
    --panel:#1a1d24;
    --text:#eaeaf0;
    --muted:#9aa0aa;
    --accent:#ff3b3b;
    --border:#2a2e38;
}

*{box-sizing:border-box}

body{
    margin:0;
    font-family:system-ui,-apple-system,BlinkMacSystemFont,sans-serif;
    background:var(--bg);
    color:var(--text);
}

a{color:var(--accent);text-decoration:none}

header{
    background:var(--panel);
    padding:1rem;
    border-bottom:1px solid var(--border);
}

header h1{margin:0;font-size:1.2rem}

nav a{margin-right:1rem;font-size:.9rem}

.container{
    max-width:1100px;
    margin:auto;
    padding:1rem;
}

.card{
    background:var(--panel);
    border:1px solid var(--border);
    border-radius:6px;
    padding:1rem;
    margin-bottom:1rem;
}

input,button{
    width:100%;
    padding:.7rem;
    margin-bottom:.7rem;
    border-radius:5px;
    border:1px solid var(--border);
    background:#111;
    color:var(--text);
}

button{
    background:var(--accent);
    border:none;
    font-weight:bold;
    cursor:pointer;
}

footer{
    text-align:center;
    padding:1rem;
    font-size:.8rem;
    color:var(--muted);
}

@media(min-width:768px){
    header{display:flex;justify-content:space-between;align-items:center}
}
CSS
    );

    // ===============================
    // SHARED LAYOUT
    // ===============================
    writeFile('core/layout.php', <<<'PHP'
<?php
require_once __DIR__ . '/auth.php';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?= $title ?? 'Hoonhub' ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
/assets/css/style.css
</head>
<body>

<header>
    <h1>Hoonhub</h1>
    <nav>
        /public/index.phpHome</a>
        <?php if (isLoggedIn()): ?>
            /public/logout.phpLogout</a>
        <?php else: ?>
            /public/login.phpLogin</a>
            /public/register.phpRegister</a>
        <?php endif; ?>
    </nav>
</header>

<main class="container">
<?= $content ?>
</main>

<footer>
    Community-first NZ automotive hub
</footer>

</body>
</html>
PHP
    );

    // ===============================
    // INDEX
    // ===============================
    writeFile('public/index.php', <<<'PHP'
<?php
ob_start();
$title = 'Home';
?>
<div class="card">
    <h2>Welcome to Hoonhub</h2>
    <p>A community-first automotive platform.</p>
</div>
<?php
$content = ob_get_clean();
require __DIR__ . '/../core/layout.php';
PHP
    );

    // ===============================
    // REGISTER
    // ===============================
    writeFile('public/register.php', <<<'PHP'
<?php
require __DIR__ . '/../core/database.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $pdo->prepare(
        'INSERT INTO users (username,email,password) VALUES (?,?,?)'
    );
    $stmt->execute([
        $_POST['username'],
        $_POST['email'],
        password_hash($_POST['password'], PASSWORD_DEFAULT)
    ]);
    header('Location: login.php');
    exit;
}

ob_start();
$title = 'Register';
?>
<div class="card">
    <h2>Create account</h2>
    <form method="post">
        <input name="username" placeholder="Username" required>
        <input name="email" placeholder="Email" required>
        <input type="password" name="password" placeholder="Password" required>
        <button>Create account</button>
    </form>
</div>
<?php
$content = ob_get_clean();
require __DIR__ . '/../core/layout.php';
PHP
    );

    // ===============================
    // LOGIN
    // ===============================
    writeFile('public/login.php', <<<'PHP'
<?php
require __DIR__ . '/../core/database.php';
require __DIR__ . '/../core/auth.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $pdo->prepare('SELECT * FROM users WHERE username = ?');
    $stmt->execute([$_POST['username']]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($user && password_verify($_POST['password'], $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        header('Location: index.php');
        exit;
    }
}

ob_start();
$title = 'Login';
?>
<div class="card">
    <h2>Login</h2>
    <form method="post">
        <input name="username" placeholder="Username" required>
        <input type="password" name="password" placeholder="Password" required>
        <button>Login</button>
    </form>
</div>
<?php
$content = ob_get_clean();
require __DIR__ . '/../core/layout.php';
PHP
    );

    echo "<h2>Upgrade complete. DELETE upgrade.php.</h2>";
    exit;
}
?>

<!doctype html>
<html>
<head><title>Hoonhub Upgrade</title></head>
<body>
<h2>Hoonhub Upgrade</h2>
<form method="post">
<button>Run Upgrade</button>
</form>
</body>
</html>
