<?php

namespace App\Modules\Production\Observers;

use App\Modules\Production\Models\ProductionOrder;
use App\Modules\Production\Notifications\NewProductionOrder;
use App\Modules\Core\Models\User;
use Illuminate\Support\Facades\Notification;

class ProductionOrderObserver
{
    /**
     * Handle the ProductionOrder "created" event.
     */
    public function created(ProductionOrder $productionOrder): void
    {
        // 找出有檢視生產工單權限的使用者
        $users = User::permission('production_orders.view')->get();

        $creatorName = $productionOrder->user ? $productionOrder->user->name : '系統';

        if ($users->isNotEmpty()) {
            Notification::send($users, new NewProductionOrder($productionOrder, $creatorName));
        }
    }
}
