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

    if($_SERVER['REQUEST_METHOD'] === 'POST') {
        $fullName = trim($_POST['full_name']);
        $email = trim($_POST['email']);   
        $phone = trim($_POST['phone']);   
        $password = trim(password_hash($_POST['password'], PASSWORD_BCRYPT));
        $referralCode = trim($_POST['referral'] ?? '');

        $stmt = $conn->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            echo json_encode(["success" => false, "message" => "Email already registered."]);
        } else {
            $stmt = $conn->prepare("INSERT INTO users (full_name, email, phone, password) VALUES (?, ?, ?, ?)");
            $stmt->bind_param("ssss", $fullName, $email, $phone, $password);
            if ($stmt->execute()) {
            echo json_encode(["success" => true, "message" => "Account created successfully."]);
            } else {
                echo json_encode(["success" => false, "message" => "Something went wrong. Try again."]);
            }
        }
        $stmt->close();
        $conn->close();
    }
?>