<?php

namespace App\Listeners;

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

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

    public function handle(OrderPacked $event): void
    {
        $order = $event->order;
        $message = "Order packed: Order #{$order->order_number}\n" .
            "Dimensions: {$event->width}cm × {$event->length}cm\n" .
            "Weight: {$event->weight}kg";

        // Notify Slack #shipping
        $this->slack->shipping($message);

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

        // Move Trello card to Packing list
        if ($order->trello_card_id) {
            $this->trello->moveCard($order->trello_card_id, 'Packing');

            // Try to check off "Packed" item in checklist
            $this->trello->checkItem($order->trello_card_id, 'Packing', 'Packed');

            Log::info('Trello card moved to Packing', ['order_uuid' => $order->uuid]);
        }
    }
}
