<?php

namespace App\Notifications;

use App\Models\LoanDisbursement;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

class LoanDisbursedNotification extends Notification
{
    use Queueable;

    public function __construct(public LoanDisbursement $disbursement) {}

    public function via(object $notifiable): array
    {
        return ['database'];
    }

    public function toDatabase(object $notifiable): array
    {
        $this->disbursement->loadMissing('loan');

        return [
            'event' => 'loan_disbursed',
            'loan_id' => $this->disbursement->loan_id,
            'loan_number' => $this->disbursement->loan->loan_number,
            'disbursement_id' => $this->disbursement->id,
            'disbursement_number' => $this->disbursement->disbursement_number,
            'amount' => (string) $this->disbursement->amount,
            'disbursement_date' => $this->disbursement->disbursement_date->toDateString(),
            'message' => 'Your loan '.$this->disbursement->loan->loan_number.' has been disbursed successfully.',
        ];
    }

    public function toArray(object $notifiable): array
    {
        return $this->toDatabase($notifiable);
    }
}
