<?php

namespace App\Notifications;

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

class AdminInAppNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        private string $notificationType,
        private string $title,
        private string $message,
        private string $severity = 'info',
        private ?string $url = null,
        private ?string $icon = null,
        private array $extra = [],
    ) {}

    public function via(object $notifiable): array
    {
        return ['database'];
    }

    public function toArray(object $notifiable): array
    {
        return [
            'notification_type' => $this->notificationType,
            'title' => $this->title,
            'message' => $this->message,
            'severity' => $this->severity,
            'icon' => $this->icon ?? self::iconForType($this->notificationType),
            'url' => $this->url,
            'extra' => $this->extra,
        ];
    }

    public static function iconForType(string $type): string
    {
        return match (true) {
            str_starts_with($type, 'provisioning_') => 'fas fa-server',
            str_starts_with($type, 'invoice_') => 'fas fa-file-invoice',
            str_starts_with($type, 'order_') => 'fas fa-shopping-cart',
            str_starts_with($type, 'ticket_') => 'fas fa-ticket-alt',
            str_starts_with($type, 'domain_') => 'fas fa-globe',
            str_starts_with($type, 'payment_') => 'fas fa-credit-card',
            str_starts_with($type, 'product_') => 'fas fa-box',
            str_starts_with($type, 'cancellation_') => 'fas fa-times-circle',
            str_starts_with($type, 'client_') => 'fas fa-user',
            default => 'fas fa-bell',
        };
    }

    public static function severityForType(string $type): string
    {
        return match (true) {
            str_contains($type, 'failed') || str_contains($type, 'escalated') || str_contains($type, 'expired') => 'danger',
            str_contains($type, 'warning') || str_contains($type, 'overdue') || str_contains($type, 'suspended') => 'warning',
            str_contains($type, 'paid') || str_contains($type, 'completed') || str_contains($type, 'approved') => 'success',
            default => 'info',
        };
    }
}
