<?php

namespace App\Events;

use App\Models\Tenant\WorkOrder;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

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

    public function __construct(public WorkOrder $workOrder, public string $tenantSlug) {}

    public function broadcastOn(): array
    {
        return [new PrivateChannel('tenant.' . $this->tenantSlug)];
    }

    public function broadcastAs(): string
    {
        return 'work-order.updated';
    }

    public function broadcastWith(): array
    {
        return [
            'id' => $this->workOrder->id,
            'number' => $this->workOrder->number,
            'status' => $this->workOrder->status,
            'pay_status' => $this->workOrder->pay_status,
            'total' => (float) $this->workOrder->total,
            'updated_at' => $this->workOrder->updated_at?->toIso8601String(),
        ];
    }
}
