<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Kreait\Firebase\Messaging\CloudMessage;
use App\Models\User;
use App\Models\UserDevice;

class FcmChannel
{
    /**
     * Send the given notification.
     */
    public function send(object $notifiable, Notification $notification): void
    {
        // $payload = $notification->toFcm($notifiable);

        // if ($notifiable->fcm_token != null){
        //     $this->sendNotification($payload, $notifiable->fcm_token, $notifiable);
        // }

        // foreach($notifiable->devices as $device){
        //     if ($device->fcm_token == $notifiable->fcm_token)
        //         continue;

        //     $this->sendNotification($payload, $device->fcm_token, $device);
        //     sleep(0.5);
        // }
    }

    private function sendNotification($payload, $fcm_token, $device){

        // if (!$fcm_token)
        //     return;

        // try{
        //     $payload["token"] = $fcm_token;
        //     $messaging = app('firebase.messaging');
        //     $message = CloudMessage::fromArray($payload);
        //     $result = $messaging->send($message);
        // }catch(\Kreait\Firebase\Exception\Messaging\NotFound $e){
        //     if (get_class($device) == UserDevice::class){
        //         $device->fcm_token = null;
        //         $device->save();
        //     }else if (get_class($device) == User::class){
        //         $device->fcm_token = null;
        //         $device->save();
        //     }
        // }catch(\Kreait\Firebase\Exception\Messaging\InvalidMessage $e){
        //     \Log::info([$fcm_token, $e->getMessage()]);
        // }catch(\Exception $e){
        //     report($e);
        // }

    }
}
