<?php
session_start();
require_once __DIR__ . '/config/db.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';

    if (!empty($name) && !empty($email) && !empty($password)) {
        // Check if user already exists
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = :email LIMIT 1");
        $stmt->execute(['email' => $email]);

        if ($stmt->fetch()) {
            $error = 'Email is already registered.';
        } else {
            // Save plain text password directly
            $insert = $pdo->prepare("INSERT INTO users (name, email, password) VALUES (:name, :email, :password)");
            $result = $insert->execute([
                'name' => $name,
                'email' => $email,
                'password' => $password
            ]);

            if ($result) {
                $success = 'Account created successfully! You can now log in.';
            } else {
                $error = 'Something went wrong. Please try again.';
            }
        }
    } else {
        $error = 'Please fill in all fields.';
    }
}
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Register</title>
    <style>
      :root {
        color-scheme: light;
        --jh-font-family-sans: "Roboto Flex", Roboto, Helvetica, Arial, sans-serif;
        --jh-font-family-primary: var(--jh-font-family-sans);
        --jh-color-blue-600: #085ce5ff;
        --jh-color-blue-950: #06185fff;
        --jh-color-gray-100: #ebebebff;
        --jh-color-gray-900: #2c2c2cff;
      }
      body {
        font-family: var(--jh-font-family-primary);
        background-color: var(--jh-color-gray-100);
        color: var(--jh-color-gray-900);
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
        margin: 0;
      }
      .auth-card {
        background: #ffffff;
        padding: 32px;
        border-radius: 12px;
        box-shadow: 0px 4px 12px rgba(0,0,0,0.1);
        width: 100%;
        max-width: 400px;
      }
      .auth-card img.logo {
        display: block;
        max-width: 150px;
        margin: 0 auto 20px auto;
      }
      .auth-card h2 {
        text-align: center;
        margin-bottom: 24px;
      }
      .form-group {
        margin-bottom: 16px;
      }
      .form-group label {
        display: block;
        margin-bottom: 6px;
        font-weight: 500;
      }
      .form-group input {
        width: 100%;
        padding: 10px;
        box-sizing: border-box;
        border: 1px solid #ccc;
        border-radius: 6px;
      }
      .btn-submit {
        width: 100%;
        padding: 12px;
        background-color: var(--jh-color-blue-600);
        color: #fff;
        border: none;
        border-radius: 6px;
        font-weight: bold;
        cursor: pointer;
      }
      .btn-submit:hover {
        background-color: var(--jh-color-blue-950);
      }
      .alert {
        padding: 10px;
        background-color: #f8d7da;
        color: #721c24;
        border-radius: 6px;
        margin-bottom: 16px;
      }
      .alert-success {
        background-color: #d4edda;
        color: #155724;
      }
      .auth-link {
        text-align: center;
        margin-top: 16px;
        display: block;
      }
    </style>
  </head>
  <body>
    <div class="auth-card">
      <img src="assets/images/logo.png" alt="Logo" class="logo">
      <h2>Create Account</h2>

      <?php if (!empty($error)): ?>
        <div class="alert"><?php echo htmlspecialchars($error); ?></div>
      <?php endif; ?>

      <?php if (!empty($success)): ?>
        <div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div>
      <?php endif; ?>

      <form action="register.php" method="POST">
        <div class="form-group">
          <label for="name">Full Name</label>
          <input type="text" id="name" name="name" required>
        </div>

        <div class="form-group">
          <label for="email">Email Address</label>
          <input type="email" id="email" name="email" required>
        </div>

        <div class="form-group">
          <label for="password">Password</label>
          <input type="password" id="password" name="password" required>
        </div>

        <button type="submit" class="btn-submit">Register</button>
      </form>

      <a href="login.php" class="auth-link">Already have an account? Log in here</a>
    </div>
  </body>
</html>