<?php
session_start();
require 'db.php';

$errorMsg = '';
$successMsg = '';
$showForm = true;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $password = $_POST['password'];
    $password2 = $_POST['password2'];

    // Basic validations
    if (empty($name) || empty($email) || empty($password) || empty($password2)) {
        $errorMsg = '❌ Please fill in all fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errorMsg = '❌ Please enter a valid email address.';
    } elseif ($password !== $password2) {
        $errorMsg = '❌ Passwords do not match.';
    } elseif (strlen($password) < 6) {
        $errorMsg = '❌ Password must be at least 6 characters.';
    } else {
        // Check if email exists
        $stmt = $pdo->prepare('SELECT COUNT(*) FROM users WHERE email = ?');
        $stmt->execute([$email]);
        if ($stmt->fetchColumn() > 0) {
            $errorMsg = '❌ Email is already registered.';
        } else {
            // Create verification token
            $token = bin2hex(random_bytes(16));
            $passwordHash = password_hash($password, PASSWORD_DEFAULT);

            $insert = $pdo->prepare('INSERT INTO users (name, email, password_hash, role, is_verified, verification_token) VALUES (?, ?, ?, ?, 0, ?)');
            $insert->execute([$name, $email, $passwordHash, 'author', $token]);

            // TODO: Send verification email here with link: verify.php?token=...
            // For example, sendVerificationEmail($email, $name, $token);

            $successMsg = "✅ Registration successful! Please check your email to verify your account.";
            $showForm = false;
        }
    }
}

$page_title = 'Register';
include 'include/header.php';
?>

<style>
  .register-wrapper {
    min-height: 80vh;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .register-card {
    width: 100%;
    max-width: 460px;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 0.5rem;
    padding: 2rem 2.5rem;
    box-shadow: 0 6px 18px rgba(0,0,0,0.1);
    font-family: 'Georgia', serif;
  }
  .register-card h3 {
    font-weight: 700;
    letter-spacing: 1.1px;
    font-size: 1.8rem;
    color: #002147;
    margin-bottom: 1.8rem;
    text-align: center;
    font-family: 'Georgia', serif;
  }
  label.form-label {
    font-weight: 600;
    font-size: 0.95rem;
    color: #495057;
  }
  input.form-control {
    height: 42px;
    font-size: 1rem;
    border-radius: 0.3rem;
    border: 1px solid #ced4da;
    transition: border-color 0.2s ease;
  }
  input.form-control:focus {
    border-color: #004a99;
    box-shadow: 0 0 6px rgba(0,74,153,0.25);
  }
  .btn-register {
    background-color: #004a99;
    border-color: #004a99;
    font-weight: 700;
    font-size: 1.1rem;
    padding: 0.5rem 0;
    border-radius: 0.3rem;
    transition: background-color 0.3s ease, border-color 0.3s ease;
    width: 100%;
  }
  .btn-register:hover {
    background-color: #003d80;
    border-color: #003d80;
  }
  .alert {
    font-family: 'Segoe UI', sans-serif;
    font-weight: 600;
    font-size: 0.9rem;
  }
  .login-link {
    margin-top: 1.5rem;
    font-size: 0.95rem;
    text-align: center;
    color: #6c757d;
  }
  .login-link a {
    color: #004a99;
    font-weight: 600;
    text-decoration: none;
    transition: color 0.2s ease;
  }
  .login-link a:hover {
    color: #002a60;
    text-decoration: underline;
  }
</style>

<div class="register-wrapper">
  <div class="register-card shadow-sm">
    <h3>Create an Account</h3>

    <?php if ($errorMsg): ?>
      <div class="alert alert-danger" role="alert"><?= htmlspecialchars($errorMsg) ?></div>
    <?php elseif ($successMsg): ?>
      <div class="alert alert-success" role="alert"><?= htmlspecialchars($successMsg) ?></div>
    <?php endif; ?>

    <?php if ($showForm): ?>
    <form method="POST" novalidate>
      <div class="mb-4">
        <label for="name" class="form-label">Full Name</label>
        <input 
          type="text" 
          id="name" 
          name="name" 
          class="form-control" 
          required 
          value="<?= isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>"
          autofocus
        >
      </div>

      <div class="mb-4">
        <label for="email" class="form-label">Email Address</label>
        <input 
          type="email" 
          id="email" 
          name="email" 
          class="form-control" 
          required 
          value="<?= isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>"
        >
      </div>

      <div class="mb-4">
        <label for="password" class="form-label">Password</label>
        <input 
          type="password" 
          id="password" 
          name="password" 
          class="form-control" 
          required
        >
      </div>

      <div class="mb-4">
        <label for="password2" class="form-label">Confirm Password</label>
        <input 
          type="password" 
          id="password2" 
          name="password2" 
          class="form-control" 
          required
        >
      </div>

      <button type="submit" class="btn btn-register">Register</button>
    </form>
    <?php endif; ?>

    <div class="login-link">
      Already have an account? <a href="login.php">Login here</a><br/>
      Forgot your password? <a href="forgot_password.php">Reset it</a>
    </div>
  </div>
</div>

<?php
include 'include/footer.php';
?>

Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'is_verified' in 'field list' in C:\xampp\htdocs\v\registration.php:36 Stack trace: #0 C:\xampp\htdocs\v\registration.php(36): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\v\registration.php on line 36