<?php

namespace App\Notifications;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\BroadcastMessage;

class MessageSent extends Notification
{
    use Queueable;

    public $user_id, $message, $name_user;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
        $user_id_to = $message->user_id_to;
        $user = User::find($user_id_to);
        $this->name_user = $user->first_name . ' ' . $user->last_name;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail','database','broadcast'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        /*
        return (new MailMessage)
                    ->subject($this->message->title)
                    ->greeting('Hola ' . $this->name_user . '!')
                    ->line($this->message->body)
                    ->action($this->message->button_text, $this->message->url)
                    ->line('Gracias por usar nuestra aplicación!');
                    if ($this->message->attached_file =! null)
                    ->attach($this->message->attached_file);
        */

        $messageMail = new MailMessage;
        $messageMail->subject($this->message->title);
        $messageMail->greeting('Hola ' . $this->name_user . '!');
        $messageMail->line($this->message->body);
        $messageMail->action($this->message->button_text, $this->message->url);
        $messageMail->line('Gracias por usar nuestra aplicación!');
        /*
        if ($this->message->attached_file =! null){
            $messageMail->attach($this->message->attached_file);
        }
        */
        return $messageMail;

    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        $notifiable->notification += 1;
        $notifiable->save();

        return [
            'url' => $this->message->url,
            'message' => $this->message->title
        ];
    }

    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            'url' => $this->message->url,
            'message' => $this->message->title
        ]);
    }

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