<?php

namespace App\Events;

use App\Models\Document;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class DocumentGenerated
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(public Document $document)
    {
        // Fire off preview generation immediately if enabled without waiting for listener chaining
        $settings = app(\App\Services\Documents\DocumentSettings::class)->get();
        if ($settings->preview_enabled) {
            try {
                dispatch(new \App\Jobs\GenerateDocumentPreview($document->id));
            } catch (\Throwable $e) {
                \Log::warning('Failed to dispatch preview job on event', [
                    'document_id' => $document->id,
                    'error' => $e->getMessage(),
                ]);
            }
        }
    }
}
