<?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 = "ePQqORzwQ9yIWLvHQyHT7Z:APA91bHX3DTAgbIUSfnSSO3y2n1dRp5Ifs9NZqU80uui-lriAyye9qdjEtBZhpEBDU6ai0LU18zrsBGVS-_xN6s8HYV-_2jxi13xzqZbv0RvVv_24572ddE";

// 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();
}

?>
