<?php

namespace App\Notifications;

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

class BillingFailureNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public InvoiceBillingJob $job;

    public string $errorMessage;

    /**
     * Create a new notification instance.
     */
    public function __construct(InvoiceBillingJob $job, string $errorMessage)
    {
        $this->job = $job;
        $this->errorMessage = $errorMessage;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        $module = $this->job->billingModule;
        $invoice = $this->job->invoice;

        return (new MailMessage)
            ->error()
            ->subject(__('admin/common.notifications_billing_failure_subject', ['module' => ($module->display_name ?? $module->name)]))
            ->line(__('admin/common.notifications_billing_failure_line1'))
            ->line(__('admin/common.notifications_billing_failure_module', ['module' => ($module->display_name ?? $module->name)]))
            ->line(__('admin/common.notifications_billing_failure_invoice', ['invoice' => ($invoice->invoice_number ?? '#'.$invoice->id)]))
            ->line(__('admin/common.notifications_billing_failure_error', ['error' => $this->errorMessage]))
            ->line(__('admin/common.notifications_billing_failure_date', ['date' => now()->format('d.m.Y H:i:s')]))
            ->action(__('admin/common.notifications_billing_failure_action'), route('admin.invoice-billings.index'))
            ->line(__('admin/common.notifications_billing_failure_line2'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            'job_id' => $this->job->id,
            'module_name' => $this->job->billingModule->name,
            'invoice_id' => $this->job->invoice_id,
            'error_message' => $this->errorMessage,
            'timestamp' => now(),
        ];
    }
}
