<?php
// =====================================================
// POST /add-users.php
// Body (JSON): { users: [ { recharge_id, user_id, username, name, address, phone } ] }
// Response: { status, message }
// =====================================================
require_once 'config.php';

$conn = getConnection();

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

if (!isset($data['users']) || !is_array($data['users'])) {
    echo json_encode(["status" => "error", "message" => "Invalid data format"]);
    exit;
}

$users = $data['users'];
$inserted = 0;

$stmt = $conn->prepare("INSERT INTO users (recharge_id, user_id, username, name, address, phone) VALUES (?, ?, ?, ?, ?, ?)");

foreach ($users as $user) {
    $recharge_id = $user['recharge_id'] ?? '';
    $user_id     = $user['user_id']     ?? '';
    $username    = $user['username']    ?? '';
    $name        = $user['name']        ?? '';
    $address     = $user['address']     ?? '';
    $phone       = $user['phone']       ?? '';

    $stmt->bind_param("ssssss", $recharge_id, $user_id, $username, $name, $address, $phone);
    if ($stmt->execute()) {
        $inserted++;
    }
}

$stmt->close();
$conn->close();

echo json_encode([
    "status"  => "success",
    "message" => "$inserted users added successfully"
]);
