<?php

namespace App\Listeners\Package;

use App\Events\Package\PackageCreated;
use App\Jobs\SendNotificationJob;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

/**
 * Listener untuk PackageCreated event
 *
 * Send notifications when package is created
 */
class SendPackageCreatedNotification implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Handle the event
     */
    public function handle(PackageCreated $event): void
    {
        $package = $event->package;
        $ppk = $package->ppk;

        // Send notification to PPK
        SendNotificationJob::dispatch(
            $ppk->id,
            'Paket ' . $package->name . ' berhasil dibuat',
            'package_created',
            ['package_id' => $package->id]
        );

        // Send notification to all agents
        $agents = User::where('role', 'agent')->get();
        foreach ($agents as $agent) {
            SendNotificationJob::dispatch(
                $agent->id,
                'Paket baru: ' . $package->name,
                'package_created',
                ['package_id' => $package->id]
            );
        }
    }
}