<?php

namespace App\Services;

use App\Models\User;
use Filament\Notifications\Notification;

class NotificationService
{
    public static function send(User|iterable $recipients, string $title, string $body, string $color = 'info', ?string $url = null): void
    {
        $notification = Notification::make()
            ->title($title)
            ->body($body)
            ->color($color);

        if ($url) {
            $notification->actions([
                \Filament\Actions\Action::make('lihat')
                    ->label('Lihat')
                    ->url($url),
            ]);
        }

        $notification->sendToDatabase(
            $recipients instanceof User ? collect([$recipients]) : collect($recipients)
        );
    }

    public static function toRole(string $role, string $title, string $body, string $color = 'info', ?string $url = null): void
    {
        $users = User::role($role)->get();
        if ($users->isEmpty()) return;
        self::send($users, $title, $body, $color, $url);
    }

    public static function toAll(string $title, string $body, string $color = 'info', ?string $url = null): void
    {
        self::send(User::all(), $title, $body, $color, $url);
    }
}
