<?php

namespace App\Jobs;

use App\Events\SendWebSocketMessageEvent;
use App\Models\Order;
use App\Services\GenerateDocumentsService;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;

class GenerateInstallationPack implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new job instance.
     */
    public function __construct(private readonly Order $order, private readonly int $userId)
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
            $link = (new GenerateDocumentsService())->generateInstallationPack($this->order, $this->userId);
            Log::info('Generate installation pack finished!');
            event(new SendWebSocketMessageEvent('Пакет документов для монтажа готов!', $this->userId, ['success' => true, 'link' => $link]));
        } catch (Exception $e) {
            Log::info('Generate installation pack failed! ' . $e->getMessage());
            event(new SendWebSocketMessageEvent('Ошибка создания покета документов для монтажа! ', $this->userId, ['error' => $e->getMessage()]));
        }



    }
}
