<?php
declare(strict_types=1);
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/functions.php';
$error = ''; $notice = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = (string)($_POST['password'] ?? '');
    if ($username === '' || $email === '' || strlen($password) < 6) {
        $error = 'Please complete all fields. Password must be at least 6 characters.';
    } else {
        $stmt = $pdo->prepare('SELECT id FROM users WHERE username = ? OR email = ? LIMIT 1');
        $stmt->execute([$username, $email]);
        if ($stmt->fetch()) { $error = 'That username or email is already registered.'; }
        else {
            $count = (int)$pdo->query('SELECT COUNT(*) FROM users')->fetchColumn();
            $role = $count === 0 ? 'admin' : 'brother';
            $stmt = $pdo->prepare('INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)');
            $stmt->execute([$username, $email, password_hash($password, PASSWORD_DEFAULT), $role]);
            $notice = 'Account created. You may now login.';
        }
    }
}
?>
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Create Account</title><link rel="stylesheet" href="assets/css/styles.css?v=300"></head><body><div class="container"><div class="form-card"><div style="text-align:center;margin-bottom:20px;"><img src="/men/images/iamyielded.jpg?v=300" alt="I Am Yielded" style="max-width:320px;width:100%;border-radius:8px;"></div><h1 style="font-size:2rem;">Create Account</h1><?php if ($error): ?><div class="error"><?php echo h($error); ?></div><?php endif; ?><?php if ($notice): ?><div class="notice"><?php echo h($notice); ?></div><?php endif; ?><form method="post"><label>Username</label><input name="username" required><label>Email</label><input name="email" type="email" required><label>Password</label><input name="password" type="password" required><div class="btn-row"><button class="btn btn-primary" type="submit">Create Account</button><a class="btn btn-secondary" href="login.php">Back to Login</a></div></form></div></div></body></html>
