<?php

declare(strict_types=1);

namespace App\Notifications;

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

class PaymentSucceededNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public PaymentTransaction $transaction,
    ) {}

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

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

        return (new MailMessage)
            ->subject("Payment of {$currency} {$amount} Received")
            ->greeting("Hello {$notifiable->name}!")
            ->line("We've successfully processed your payment of **{$currency} {$amount}**.")
            ->line("Payment method: {$this->transaction->payment_method}")
            ->line("Transaction ID: {$this->transaction->gateway_transaction_id}")
            ->action('View Billing History', $billingUrl)
            ->line('Thank you for choosing EZSCALE!');
    }

    /** @return array<string, mixed> */
    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'payment_succeeded',
            'transaction_id' => $this->transaction->id,
            'amount' => $this->transaction->amount,
            'currency' => $this->transaction->currency,
            'gateway' => $this->transaction->gateway,
            'message' => "Payment of {$this->transaction->currency} {$this->transaction->amount} processed successfully.",
        ];
    }
}
