<?php

namespace App\Listeners;

use App\Events\ParcelDelivered;
use App\Services\SlackNotifierService;
use App\Services\TrelloService;
use Illuminate\Support\Facades\Log;

class NotifySlackOnParcelDelivered
{
    public function __construct(
        protected SlackNotifierService $slack,
        protected TrelloService $trello,
    ) {
    }

    public function handle(ParcelDelivered $event): void
    {
        $order = $event->order;
        $message = "✅ Parcel delivered: Order #{$order->order_number}\n" .
            "Delivered at: {$order->delivered_at}";

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

        Log::info('Parcel delivered notification sent', ['order_uuid' => $order->uuid]);

        if ($order->trello_card_id) {
            $this->trello->moveCard($order->trello_card_id, 'Done');
        }
    }
}
