<?php

require 'vendor/autoload.php';

use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;

// Path to Firebase service account JSON file
$firebaseConfigPath = __DIR__ . '/serviceAccount.json';

// Initialize Firebase
$factory = (new Factory)->withServiceAccount($firebaseConfigPath);
$messaging = $factory->createMessaging();

// FCM Device Token (Replace with a real token from your device)
$firebaseToken = "cdVLvrG8QoGT_rronnzN2p:APA91bHWD58-lm79IOhN7sGDRQino274xGmuOWmSeYp7lgBH5QpfP000tZJEBvUkXmkFhm1dWILrgJsgrU9nvHzi60iRhAWHI5cCGAkw8Hpudb6i-r1xGf4";

// URL of the hosted audio file (Make sure this is accessible)
$audioUrl = "https://file-examples.com/storage/fe7f95684167ed3aeaaf793/2017/11/file_example_MP3_700KB.mp3";

// Create a Firebase notification (without withSound())
$notification = Notification::create('Loss', 'You are lossed 1lakh, please contact adarsh');

// Create a Cloud Message with data for the frontend to play audio
$message = CloudMessage::withTarget('token', $firebaseToken)
    ->withNotification($notification)
    ->withData([
        'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
        'audio_url' => $audioUrl  // Send audio URL to frontend
    ]);

// Send the notification
try {
    $messaging->send($message);
    echo "Voice notification sent successfully!";
} catch (Exception $e) {
    
    echo "Error: " . $e->getMessage();
}

?>
