<?php

namespace App\Notifications;

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

class LoanPenaltyNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $loan;
    protected $penalty;
    protected $reason;

    public function __construct($loan, $penalty, $reason = null)
    {
        $this->loan = $loan;
        $this->penalty = $penalty;
        $this->reason = $reason;
    }

    public function via($notifiable)
    {
        $channels = [];

        if ($notifiable->email_notifications) {
            $channels[] = 'mail';
        }

        if ($notifiable->sms_notifications && $notifiable->phone) {
            $channels[] = 'sms';
        }

        return $channels;
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Loan Penalty Notice - ' . config('app.name'))
            ->view('emails.loan-penalty', [
                'loan' => $this->loan,
                'penalty' => $this->penalty,
                'reason' => $this->reason,
                'client' => $notifiable
            ]);
    }

    public function toSms($notifiable)
    {
        $message = "Penalty Notice: Loan #{$this->loan->id} has a penalty of {$this->penalty}. ";
        $message .= "Reason: {$this->reason ?? 'Late payment'}";
        return $message;
    }
}
