<?php
declare(strict_types=1);
require_once __DIR__ . '/config.php';

if (!empty($_SESSION['user_id'])) {
    header('Location: dashboard.php');
    exit;
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = (string)($_POST['password'] ?? '');
    $confirm = (string)($_POST['confirm_password'] ?? '');

    if ($username === '' || $email === '' || $password === '' || $confirm === '') {
        $error = 'Please complete all fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } elseif ($password !== $confirm) {
        $error = 'Passwords do not match.';
    } elseif (strlen($password) < 8) {
        $error = 'Use at least 8 characters for the password.';
    } else {
        $check = $pdo->prepare('SELECT id FROM users WHERE username = ? OR email = ? LIMIT 1');
        $check->execute([$username, $email]);

        if ($check->fetch()) {
            $error = 'That username or email is already registered.';
        } else {
            $hash = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare('INSERT INTO users (username, email, password) VALUES (?, ?, ?)');
            $stmt->execute([$username, $email, $hash]);
            $success = 'Registration complete. You can login now.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Gotalist Register</title>
  <link rel="stylesheet" href="assets/css/styles.css" />
</head>
<body>
  <div class="container">
    <div class="form-card">
      <div class="brand" style="margin-bottom:1rem;">
        <span class="brand-mark">G</span>
        <span>Gotalist Members</span>
      </div>
      <h1 style="font-size:2rem;">Create Account</h1>
      <p class="muted">Start clean with a secure login for your members area.</p>

      <?php if ($error): ?>
        <div class="error"><?php echo h($error); ?></div>
      <?php endif; ?>

      <?php if ($success): ?>
        <div class="notice"><?php echo h($success); ?></div>
      <?php endif; ?>

      <form method="post" action="">
        <label for="username">Username</label>
        <input id="username" name="username" type="text" maxlength="60" required>

        <label for="email">Email</label>
        <input id="email" name="email" type="email" maxlength="120" required>

        <label for="password">Password</label>
        <input id="password" name="password" type="password" maxlength="100" required>

        <label for="confirm_password">Confirm Password</label>
        <input id="confirm_password" name="confirm_password" type="password" maxlength="100" required>

        <div class="btn-row">
          <button class="btn btn-primary" type="submit">Register</button>
          <a class="btn btn-secondary" href="login.php">Back to Login</a>
        </div>
      </form>
    </div>
  </div>
</body>
</html>
