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

$host = 'localhost';
$user = 'ahmeuesz_dino';
$pwd  = 'X39IW2ho3^5h6d_5';
$db   = 'ahmeuesz_myapp';
$conn = mysqli_connect($host, $user, $pwd, $db);
if (!$conn) die("DB Conn Error: " . mysqli_connect_error());

// Ensure uploads dir exists
if (!is_dir('uploads')) {
    mkdir('uploads', 0777, true);
}

$message = '';
if (isset($_POST['register'])) {
    $name     = trim($_POST['name']);
    $email    = trim($_POST['email']);
    $password = trim($_POST['password']);
    $image    = '';

    // Handle file upload (simple logic)
    if (!empty($_FILES['profile_image']['name'])) {
        $orig = basename($_FILES['profile_image']['name']);
        $ext  = strtolower(pathinfo($orig, PATHINFO_EXTENSION));
        $allowed = ['jpg','jpeg','png','gif'];
        if (in_array($ext, $allowed)) {
            $filename = uniqid() . '_' . preg_replace('/[^A-Za-z0-9_\-\.]/', '', $orig);
            $target = 'uploads/' . $filename;
            if (move_uploaded_file($_FILES['profile_image']['tmp_name'], $target)) {
                $image = $filename; // store filename only
            } else {
                $message = "❌ Failed to move uploaded file.";
            }
        } else {
            $message = "❌ Only JPG, PNG, GIF allowed.";
        }
    }

    if ($message === '') {
        // check existing email
        $checkSql = "SELECT * FROM users WHERE email = '$email'";
        $res = mysqli_query($conn, $checkSql);
        if ($res && mysqli_num_rows($res) > 0) {
            $message = "❌ This email is already registered.";
        } else {
            // insert (simple)
            $imageVal = ($image !== '') ? "'$image'" : "NULL";
            $insert = "INSERT INTO users (name, email, password, profile_image) VALUES ('$name', '$email', '$password', $imageVal)";
            if (mysqli_query($conn, $insert)) {
                // redirect to login after registration
                header("Location: login.php?registered=1");
                exit();
            } else {
                $message = "❌ DB Error: " . mysqli_error($conn);
            }
        }
    }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Register</title>
<link rel="stylesheet" href="style/form.css">
</head>
<body>
<div class="form-wrap">
  <h2>Create Account</h2>

  <?php if (!empty($message)): ?>
    <div class="msg error"><?php echo htmlspecialchars($message); ?></div>
  <?php endif; ?>

  <form method="post" enctype="multipart/form-data" class="form">
    <input type="text" name="name" placeholder="Full name" required>
    <input type="email" name="email" placeholder="Email address" required>
    <input type="password" name="password" placeholder="Password" required>

    <div class="upload-box">
      <label class="upload-label">
        <input type="file" name="profile_image" accept="image/*" class="file-input">
        <span class="upload-text">📸 Upload your profile photo</span>
        <small class="upload-hint">JPG / PNG / GIF allowed</small>
      </label>
    </div>

    <input type="submit" name="register" value="Register" class="btn">
  </form>

  <p class="foot">Already have an account? <a href="login.php">Login</a></p>
</div>
</body>
</html>
