<?php

namespace App\Domains\Analytics\Events;

use App\Domains\Analytics\Models\Click;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;

class ClickRecorded implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets;

    public function __construct(public readonly Click $click) {}

    public function broadcastOn(): Channel
    {
        return new Channel("workspace.{$this->click->workspace_id}.analytics");
    }

    public function broadcastAs(): string
    {
        return 'click.recorded';
    }

    /** @return array<string, mixed> */
    public function broadcastWith(): array
    {
        return [
            'link_id' => $this->click->link_id,
            'country' => $this->click->country,
            'device' => $this->click->device,
            'clicked_at' => $this->click->clicked_at->toISOString(),
        ];
    }
}
