<?php

declare(strict_types=1);

namespace App\Notifications;

use App\Models\SubscriptionRequest;
use Filament\Actions\Action; // Filament 5 unificó las acciones (antes Filament\Notifications\Actions\Action, inexistente en v5)
use Filament\Notifications\Notification as FilamentNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

/**
 * Notification for Subscription Request events.
 *
 * Events: created, approved, rejected, implemented
 */
class SubscriptionRequestNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public SubscriptionRequest $request,
        public string $event
    ) {}

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

    public function toDatabase(object $notifiable): array
    {
        return FilamentNotification::make()
            ->title($this->getTitle())
            ->body($this->getBody())
            ->icon($this->getIcon())
            ->iconColor($this->getIconColor())
            ->actions($this->getActions())
            ->getDatabaseMessage();
    }

    protected function getTitle(): string
    {
        return match ($this->event) {
            'created' => 'Nueva solicitud de suscripción',
            'approved' => 'Solicitud aprobada',
            'rejected' => 'Solicitud rechazada',
            'implemented' => 'Cambios implementados',
            default => 'Actualización de solicitud',
        };
    }

    protected function getBody(): string
    {
        $company = $this->request->company?->name ?? 'Empresa';
        $type = $this->request->request_type_label;

        return match ($this->event) {
            'created' => "{$company} ha solicitado: {$type}",
            'approved' => "Tu solicitud de {$type} ha sido aprobada",
            'rejected' => "Tu solicitud de {$type} ha sido rechazada",
            'implemented' => "Los cambios de {$type} han sido aplicados",
            default => "Actualización en solicitud de {$type}",
        };
    }

    protected function getIcon(): string
    {
        return match ($this->event) {
            'created' => 'heroicon-o-inbox-arrow-down',
            'approved' => 'heroicon-o-check-circle',
            'rejected' => 'heroicon-o-x-circle',
            'implemented' => 'heroicon-o-check-badge',
            default => 'heroicon-o-bell',
        };
    }

    protected function getIconColor(): string
    {
        return match ($this->event) {
            'created' => 'info',
            'approved' => 'success',
            'rejected' => 'danger',
            'implemented' => 'success',
            default => 'gray',
        };
    }

    protected function getActions(): array
    {
        $url = route('filament.admin.resources.subscription-requests.view', ['record' => $this->request->id]);

        return [
            Action::make('view')
                ->label('Ver Solicitud')
                ->url($url)
                ->markAsRead(),
        ];
    }
}
