<?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 ServiceProvisionedNotification 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
    {
        $serviceUrl = 'https://'.config('app.domains.account').'/services/'.$this->service->id;

        $mail = (new MailMessage)
            ->subject('Your Service is Ready!')
            ->greeting("Hello {$notifiable->name}!")
            ->line("Your **{$this->service->service_type}** service has been provisioned and is ready to use.");

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

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

        return $mail
            ->action('View Service Details', $serviceUrl)
            ->line('If you need help getting started, check our knowledge base or contact support.');
    }

    /** @return array<string, mixed> */
    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'service_provisioned',
            '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 is ready!",
        ];
    }
}
