<?php
header('Content-Type: application/json');
require 'db.php'; // Include your database connection script

$data = json_decode(file_get_contents('php://input'), true);

$name = $data['name'];
$phone = $data['phone'];
$email = $data['email'];
$location = $data['location'];
$address = $data['address'];
$password = password_hash($data['password'], PASSWORD_BCRYPT); // Hash the password
$referral_code = ''; // Generate or provide a referral code
$refered_code = ''; // Optional
$otp = ''; // Optional

// Check if email or phone already exists
$sql_check = "SELECT id FROM users WHERE email = '$email' OR phone = '$phone'";
$result_check = $conn->query($sql_check);

if ($result_check->num_rows > 0) {
    echo json_encode(['success' => false, 'message' => 'Email or phone number already exists']);
} else {
    $sql = "INSERT INTO users (name, phone, email, location, address, password, referral_code, refered_code, otp) VALUES ('$name', '$phone', '$email', '$location', '$address', '$password', '$referral_code', '$refered_code', '$otp')";

    if ($conn->query($sql) === TRUE) {
        echo json_encode(['success' => true, 'message' => 'User registered successfully']);
    } else {
        echo json_encode(['success' => false, 'message' => 'Error: ' . $conn->error]);
    }
}

$conn->close();
?>
