<?php

// File: app/Events/ExportCompleted.php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ExportCompleted implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $userId;
    public $filename;
    public $emailAddress;
    public $message;

    /**
     * Create a new event instance.
     */
    public function __construct($userId, $filename, $emailAddress)
    {
        $this->userId = $userId;
        $this->filename = $filename;
        $this->emailAddress = $emailAddress;
        $this->message = 'Export completato! Controlla la tua email.';
    }

    /**
     * Get the channels the event should broadcast on.
     */
    public function broadcastOn()
    {
        return new PrivateChannel('exports.' . $this->userId);
    }

    /**
     * Get the data to broadcast.
     */
    public function broadcastWith()
    {
        return [
            'type' => 'export_completed',
            'message' => $this->message,
            'filename' => $this->filename,
            'email' => $this->emailAddress,
            'timestamp' => now()->toISOString()
        ];
    }

    /**
     * The event's broadcast name.
     */
    public function broadcastAs()
    {
        return 'export.completed';
    }
}
