<?php

declare(strict_types=1);

namespace App\Notifications;

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

class PaymentFailedNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public string $gateway,
        public float $amount,
        public string $currency,
        public string $reason,
    ) {}

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

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

        return (new MailMessage)
            ->subject('Payment Failed - Action Required')
            ->greeting("Hello {$notifiable->name},")
            ->line("We were unable to process your payment of **{$currency} {$amount}**.")
            ->line("Reason: {$this->reason}")
            ->line('Please update your payment method to avoid service interruption.')
            ->action('Update Payment Method', $billingUrl)
            ->line('If you need assistance, please contact our support team.');
    }

    /** @return array<string, mixed> */
    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'payment_failed',
            'amount' => $this->amount,
            'currency' => $this->currency,
            'gateway' => $this->gateway,
            'reason' => $this->reason,
            'message' => "Payment of {$this->currency} {$this->amount} failed: {$this->reason}",
        ];
    }
}
