<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use App\Models\User;

class DeleteUserRequest extends Notification
{
    use Queueable;

    /**
     * The delete user request token.
     *
     * @var string
     */
    public $token;


    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

    /**
     * Create a notification instance.
     *
     * @param  string  $token
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

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

    /**
     * Build the mail representation of the notification.
     *
     * @param  User  $notifiable
     * @return MailMessage
     */
    public function toMail(User $notifiable)
    {
        $this->locale($notifiable->language_id);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $this->token);
        }

        $subject = trans('emails.app-delete-user-request.subject', [], $notifiable->language_id);
        $title = trans('emails.app-delete-user-request.title', [], $notifiable->language_id);
        $intro   = trans('emails.app-delete-user-request.intro', [
            'name' => $notifiable->name
        ], $notifiable->language_id);
        $action  = trans('emails.app-delete-user-request.cta', [], $notifiable->language_id);
        $expire  = trans('emails.app-delete-user-request.expires', [
            'count' => '60'
        ], $notifiable->language_id);
        $ignore  = trans('emails.app-delete-user-request.ignore', [], $notifiable->language_id);
        $context = trans('emails.app-delete-user-request.context', [], $notifiable->language_id);
        $regards = trans('emails.app-delete-user-request.regards', [], $notifiable->language_id);
        $team = trans('emails.app-delete-user-request.team', [], $notifiable->language_id);

        $link = config('app.frontend_url') . '/' . $notifiable->language_id . '/delete-user/' . $this->token;

        return (new MailMessage)
            ->subject($subject)
            ->greeting($title)
            ->line($intro)
            ->line($context)
            ->action($action, $link)
            ->line($expire)
            ->line($ignore)
            ->line($regards)
            ->line($team);
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}
