<?php

namespace App\Notifications;

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

class LoanOverdueNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $loan;
    protected $repayment;
    protected $penalty;

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

    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('Overdue Loan Payment Notice - ' . config('app.name'))
            ->view('emails.loan-overdue', [
                'loan' => $this->loan,
                'repayment' => $this->repayment,
                'penalty' => $this->penalty,
                'client' => $notifiable
            ]);
    }

    public function toSms($notifiable)
    {
        $message = "Overdue Notice: Loan #{$this->loan->id} payment is overdue. ";
        if ($this->repayment) {
            $message .= "Amount: {$this->repayment->amount}. ";
            $message .= "Penalty: {$this->penalty}";
        }
        return $message;
    }
}
