<?php

namespace App\Listeners;

use App\Events\ParcelFailedDelivery;
use App\Services\SlackNotifierService;
use Illuminate\Support\Facades\Log;

class NotifySlackOnParcelFailedDelivery
{
    public function __construct(protected SlackNotifierService $slack)
    {
    }

    public function handle(ParcelFailedDelivery $event): void
    {
        $order = $event->order;
        $message = "⚠️ Parcel delivery failed: Order #{$order->order_number}\n" .
            "Reason: {$event->failureReason}";

        $this->slack->opsAlerts($message);

        Log::warning('Parcel delivery failed', [
            'order_id' => $order->id,
            'reason' => $event->failureReason,
        ]);
    }
}
