<?php

namespace App\Listeners;

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

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

    public function handle(ShipmentCreationFailed $event): void
    {
        $order = $event->order;
        $message = "🚨 Shipment creation failed – Order #{$order->order_number}\n" .
            "Reason: {$event->errorMessage}";

        // Alert ops team
        $this->slack->opsAlerts($message);

        Log::error('Shipment creation failed', [
            'order_id' => $order->id,
            'error' => $event->errorMessage,
        ]);
    }
}
