<?php

namespace App\Notifications;

use App\Helpers\NotificationHelper;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;

class PushNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $user;
    public $message;
    public $description;
    public $image;

    /**
     * Create a new notification instance.
     */
    public function __construct($user, $message, $description, $image)
    {
        $this->user = $user;
        $this->message = $message;
        $this->description = $description;
        $this->image = $image;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['broadcast', 'customPush'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toBroadcast(object $notifiable)
    {
        return new BroadcastMessage([
            'message' => $this->message,
            'description' => $this->description,
            'image' => $this->image,
        ]);
    }

    public function toCustomPush($notifiable)
    {
        Log::info("Sending push notification for User ID {$notifiable->id}");

        // NotificationHelper::sendAndStoreNotification(
        //     $this->user->fcm_token,
        //     $this->user->id,
        //     $this->message,
        //     $this->description
        // );

        NotificationHelper::sendAndStoreNotificationNew(
            $this->user->fcms,
            $this->user->id,
            $this->message,
            $this->description
        );
    }

    // /**
    //  * Get the array representation of the notification.
    //  *
    //  * @return array<string, mixed>
    //  */
    // public function toArray(object $notifiable): array
    // {
    //     return [
    //         //
    //     ];
    // }

    public function handle()
    {
        Log::info("i am in notifications");
        // Here you can define the logic to send the actual push notification
        // Example: using a third-party service (Firebase, Pusher, etc.)
        // $campaign = Campaign::with('campaignAudience')->find($this->campaignId);
        NotificationHelper::sendAndStoreNotification(
            $this->user->fcm_token,
            $this->user->id,
            $this->message,
            $this->description,
        );
    }
}
