<?php
require 'c:/xampp/htdocs/dizzartshopping/shopnest/admin/includes/fcm_helper.php';
require 'c:/xampp/htdocs/dizzartshopping/shopnest/php-api-server/config/database.php';

$db = getDb();
// Let's get the token for t6@gmail.com
$stmt = $db->prepare("SELECT fcm_token FROM users WHERE email = 't6@gmail.com' LIMIT 1");
$stmt->execute();
$res = $stmt->get_result()->fetch_assoc();
$device_token = $res['fcm_token'];

echo "Device Token: " . $device_token . "\n\n";

if (empty($device_token)) {
    die("No device token found for t6@gmail.com");
}

$title = "Test Error Logging";
$body = "Trying to find out why FCM fails";

$server_token = getFCMServerToken();
echo "Generated Server Token length: " . strlen($server_token) . "\n\n";

$url = "https://fcm.googleapis.com/v1/projects/dizaartshopping/messages:send";

$data = [
    "message" => [
        "token" => $device_token,
        "notification" => [
            "title" => $title,
            "body" => $body
        ]
    ]
];

$headers = [
    'Authorization: Bearer ' . $server_token,
    'Content-Type: application/json'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);

echo "HTTP Code: " . $httpcode . "\n";
echo "cURL Error: " . $error . "\n";
echo "FCM Response: " . $result . "\n";
?>
