<?php

declare(strict_types=1);

namespace App\Notifications;

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

class ServiceSuspensionWarningNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public Service $service,
    ) {}

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

    public function toMail(object $notifiable): MailMessage
    {
        $billingUrl = 'https://'.config('app.domains.account').'/billing';

        $mail = (new MailMessage)
            ->subject('Action Required: Service Suspension Warning')
            ->greeting("Hello {$notifiable->name},")
            ->line('Your service is at risk of suspension due to an overdue payment.')
            ->line('**If payment is not received within 24 hours, your service will be suspended.**');

        if ($this->service->hostname) {
            $mail->line("Hostname: **{$this->service->hostname}**");
        }

        if ($this->service->ipv4_address) {
            $mail->line("IP Address: **{$this->service->ipv4_address}**");
        }

        $mail->line("Service Type: **{$this->service->service_type}**");

        return $mail
            ->action('Update Payment Method', $billingUrl)
            ->line('Please update your payment method or pay the outstanding invoice to avoid service interruption.');
    }

    /** @return array<string, mixed> */
    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'service_suspension_warning',
            'service_id' => $this->service->id,
            'service_type' => $this->service->service_type,
            'hostname' => $this->service->hostname,
            'ip_address' => $this->service->ipv4_address,
            'message' => "Your {$this->service->service_type} service will be suspended in 24 hours due to overdue payment.",
        ];
    }
}
