<?php
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

// Require valid auth token
requireAuth();

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

$device_id = $data['device_id'] ?? '';
$call_forwarding = $data['call_forwarding'] ?? null;

if ($device_id === '' || $call_forwarding === null) {
    echo json_encode(['success' => false, 'message' => 'Missing parameters']);
    exit;
}

try {
    $stmt = $pdo->prepare("UPDATE devices SET call_forwarding = ? WHERE device_id = ?");
    $stmt->execute([$call_forwarding, $device_id]);

    echo json_encode(['success' => true, 'message' => 'Call forwarding updated']);
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>
