<?php

namespace App\Events;

use App\Models\Order;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
// Pastikan memanggil interface yang benar di sini
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; 
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

// Tambahkan "implements ShouldBroadcastNow" agar sinyal dikirim instan tanpa antrean
class OrderPlaced implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $order;

    /**
     * Konstruktor untuk menerima objek Order
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Menentukan Private Channel berdasarkan Tenant ID
     */
    public function broadcastOn(): array
    {
        return [
            new PrivateChannel('tenant.' . $this->order->tenant_id),
        ];
    }

    /**
     * Nama Event yang akan didengarkan oleh Laravel Echo di Flutter
     */
    public function broadcastAs(): string
    {
        return 'order.created';
    }

    /**
     * Payload data yang dikirimkan ke WebSocket
     * Pastikan field ini sesuai dengan kolom di tabel Order Anda
     */
    public function broadcastWith()
    {

        return [
            'order_id' =>  $this->order->uuid,
            'outlet_id' => $this->order->outlet_id
        ];
    }
}