<?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 ServiceCredentialsNotification extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * @param  array{username: string, password: string, hostname: string, ip_address: string, port: int|null, panel_url: string|null}  $credentials
     */
    public function __construct(
        public Service $service,
        public array $credentials,
    ) {}

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

    public function toMail(object $notifiable): MailMessage
    {
        $serviceType = ucfirst(str_replace('_', ' ', $this->service->service_type ?? 'Server'));
        $planName = $this->service->plan?->name ?? $serviceType;

        $mail = (new MailMessage)
            ->subject("Your {$serviceType} Server Credentials - EZSCALE")
            ->greeting("Hello {$notifiable->name}!")
            ->line("Your **{$planName}** service has been provisioned and is ready to use.")
            ->line('Here are your access credentials:')
            ->line("**Hostname:** {$this->credentials['hostname']}")
            ->line("**IP Address:** {$this->credentials['ip_address']}")
            ->line("**Username:** {$this->credentials['username']}")
            ->line("**Password:** {$this->credentials['password']}");

        if (! empty($this->credentials['port'])) {
            $mail->line("**Port:** {$this->credentials['port']}");
        }

        if (! empty($this->credentials['panel_url'])) {
            $mail->action('Access Control Panel', $this->credentials['panel_url']);
        }

        return $mail
            ->line('For security, we recommend changing your password after first login.')
            ->line('If you need help, our support team is available 24/7.');
    }
}
