<?php

declare(strict_types=1);

namespace App\Listeners;

use App\Events\ProvisioningFailed;
use Illuminate\Support\Facades\Log;

class HandleProvisioningFailed
{
    public function handle(ProvisioningFailed $event): void
    {
        $service = $event->service;

        Log::warning("Provisioning failed for service #{$service->id}", [
            'service_id' => $service->id,
            'user_id' => $service->user_id,
            'service_type' => $service->service_type,
            'platform' => $service->platform,
            'error' => $event->errorMessage,
            'attempt' => $event->attemptNumber,
            'max_attempts_reached' => $event->maxAttemptsReached,
        ]);

        if ($event->maxAttemptsReached) {
            Log::error("Provisioning permanently failed for service #{$service->id} after {$event->attemptNumber} attempts", [
                'service_id' => $service->id,
                'user_id' => $service->user_id,
                'service_type' => $service->service_type,
                'platform' => $service->platform,
                'error' => $event->errorMessage,
            ]);
        }
    }
}
