<?php

declare(strict_types=1);

namespace App\Notifications;

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

class InvoiceNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public Invoice $invoice,
    ) {}

    /** @return array<int, string> */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        $amount = number_format((float) $this->invoice->total, 2);
        $currency = strtoupper($this->invoice->currency);
        $invoiceUrl = 'https://'.config('app.domains.account').'/billing/invoices';

        $mail = (new MailMessage)
            ->subject("Invoice #{$this->invoice->number} - {$currency} {$amount}")
            ->greeting("Hello {$notifiable->name},")
            ->line("Please find below the details for invoice **#{$this->invoice->number}**.")
            ->line("Amount Due: **{$currency} {$amount}**");

        if ($this->invoice->due_date) {
            $dueDate = $this->invoice->due_date->format('M j, Y');
            $mail->line("Due Date: **{$dueDate}**");
        }

        if ($this->invoice->notes) {
            $mail->line("Notes: {$this->invoice->notes}");
        }

        return $mail
            ->action('View Invoices', $invoiceUrl)
            ->line('Thank you for your business!');
    }
}
