<?php

namespace App\Broadcasting;

use App\Models\Token;
use App\Models_v2\DatabaseNotification;
use App\Models_v2\MessageRead;
use App\User;
use Illuminate\Notifications\Notification;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;

class DatabaseNotificationOld
{
    /**
     * Create a new channel instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function send($notifiable, Notification $notification)
    {
        $message = $notification->toArray($notifiable);

        $userId = $notifiable->id;
        if (isset($message['type'])) {
            $type = $message['type'];
        } else {
            $type = config('constants.SEARCH_HOSTEL');
        }
        $id = null;
        $status = 0;
        $image = null;
        $renterArr = null;

        if (isset($message['id'])) {
            $id = $message['id'];
        }

        if (isset($message['object_id'])) {
            $id = $message['object_id'];
        }

        if (isset($message['status'])) {
            $status = $message['status'];
        }

        if (isset($message['image'])) {
            $image = $message['image'];
        }

        if (isset($message['renter_id'])) {
            $renterId = $message['renter_id'];
            $renter = User::find($renterId);
            if ($renter) {
                $renterArr = [
                    'id' => $renter->id,
                    'name' => $renter->name,
                    'image' => $renter->image,
                    'type' => $renter->type
                ];
            }
        }
        $payload = json_encode([
            'type' => $type,
            'id' => $id,
            'status' => $status,
            'renter' => $renterArr
        ]);

        \App\Models\Notification::create([
            'to_user' => $userId,
            'title' => $message['title'],
            'content' => $message['body'],
            'payload' => $payload,
            'status' => \App\Models\Notification::PUSHED,
            'image' => $image
        ]);
        // Send notification to the $notifiable instance...
    }

    /**
     * Authenticate the user's access to the channel.
     *
     * @param \App\User $user
     *
     * @return array|bool
     */
    public function join(User $user)
    {
        //
    }
}
