<?php
require "db.php";

$username = trim($_POST["username"] ?? "");
$email = trim($_POST["email"] ?? "");
$password = $_POST["password"] ?? "";

if (!$username || !$email || !$password) {
    die("Missing fields.");
}

$hash = password_hash($password, PASSWORD_DEFAULT);

$stmt = $pdo->prepare("
    INSERT INTO users (username, email, password_hash, provider)
    VALUES (?, ?, ?, 'email')
");
$stmt->execute([$username, $email, $hash]);

$userId = $pdo->lastInsertId();

$_SESSION["user"] = [
    "id" => $userId,
    "username" => $username,
    "email" => $email
];

header("Location: ../Index.php");
exit;