<?php

namespace App\Modules\FINANCE\Notifications;

use App\Modules\FINANCE\Models\Invoice;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class InvoiceReminderNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $invoice;
    protected $type;

    /**
     * Create a new notification instance.
     */
    public function __construct(Invoice $invoice, $type = 'upcoming')
    {
        $this->invoice = $invoice;
        $this->type = $type;
    }

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

    /**
     * Get the mail representation of the notification.
     */
    public function toMail($notifiable): MailMessage
    {
        $url = route('public.invoice.show', $this->invoice->public_hash);
        
        $subject = match($this->type) {
            'upcoming' => 'Upcoming Payment Reminder: Invoice #' . $this->invoice->invoice_number,
            'overdue'  => 'Overdue Payment Reminder: Invoice #' . $this->invoice->invoice_number,
            default    => 'Invoice Summary: #' . $this->invoice->invoice_number,
        };

        return (new MailMessage)
                    ->subject($subject)
                    ->line('This is a reminder regarding your invoice #' . $this->invoice->invoice_number . '.')
                    ->line('Amount Due: ' . number_format($this->invoice->total_amount, 2))
                    ->line('Due Date: ' . $this->invoice->due_date->format('d M Y'))
                    ->action('View Invoice', $url)
                    ->line('Thank you for using our services!');
    }

    /**
     * Get the array representation of the notification.
     */
    public function toArray($notifiable): array
    {
        return [
            'invoice_id' => $this->invoice->id,
            'invoice_number' => $this->invoice->invoice_number,
            'amount' => $this->invoice->total_amount,
            'type' => $this->type,
        ];
    }
}
