<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

use App\Models\User;
use Illuminate\Support\Facades\Http;

class FirebaseNotification extends Notification
{
    use Queueable;
    public $title;
    public $body;

    /**
     * Create a new notification instance.
     *
     * @return void
     */

    public function __construct($firebaseToken, $title, $body)
    {
        $this->title = $title;
        $this->body = $body;
        $this->firebaseToken = $firebaseToken;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        // $firebaseTokens = User::whereNotNull('device_token')->pluck('device_token')->all();
        $SERVER_API_KEY = env('FIREBASE_SERVER_KEY');

        $data = [
            "registration_ids" => explode(",", $this->firebaseToken),
            "notification" => [
                "title" => $this->title,
                "body" => $this->body,
                "content_available" => true,
                "priority" => "high",
            ]
        ];

        $response = Http::withHeaders([
            'Authorization' => 'key=' . $SERVER_API_KEY,
            'Content-Type' => 'application/json',
        ])->post('https://fcm.googleapis.com/fcm/send', $data);
    }

    public function toFirebase($notifiable)
    {
        return [];
    }
}
