<?php

namespace App\Observers;

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

class WordOserver
{
    /**
     * Handle the Word "created" event.
     */
 public function created(Word $word): void
    {
        // Get the user who created this word
        $creator = $word->user; 

        // Fallback if not set
        $creatorName = $creator ? $creator->name : 'Someone';

        // Notify all users
        $users = User::all();

        foreach ($users as $recipient) {
            Notification::make()
                ->title("New word added: {$word->word}")
                ->body("{$creatorName} added a new word: {$word->word}")
                ->sendToDatabase($recipient);
        }
    }

    /**
     * Handle the Word "updated" event.
     */
    public function updated(Word $word): void
    {
        //
    }

    /**
     * Handle the Word "deleted" event.
     */
    public function deleted(Word $word): void
    {
        //
    }

    /**
     * Handle the Word "restored" event.
     */
    public function restored(Word $word): void
    {
        //
    }

    /**
     * Handle the Word "force deleted" event.
     */
    public function forceDeleted(Word $word): void
    {
        //
    }
}
