<?php
require_once 'config/auth.php';

if (!$AllowUserRegistration){
    echo json_encode(['message' => 'Endpoint not found']);
    http_response_code(404);
    die;
}

require_once 'config/db.php';
$data = json_decode(file_get_contents("php://input"));

if (isset($data->username) && isset($data->password) && isset($data->name)) {
    $username = $data->username;
    $password = password_hash($data->password, PASSWORD_BCRYPT);
    $name = $data->name;

    $query = "INSERT INTO users (username, password, name) VALUES (:username, :password, :name)";
    $stmt = $pdo->prepare($query);

    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $password);
    $stmt->bindParam(':name', $name);

    if ($stmt->execute()) {
        echo json_encode(['message' => 'User registered successfully']);
    } else {
        echo json_encode(['message' => 'User registration failed']);
    }
} else {
    http_response_code(400);
    echo json_encode(['message' => 'Missing name, username or password']);
}
?>
