<?php

namespace App\Jobs;

use App\Models\Customer;
use App\Models\User;
use App\Services\FcmService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

/**
 * PERF-03: FcmService::sendToOwner() used to make the FCM HTTP v1 call
 * (OAuth token exchange + one request per device token) inline on every
 * booking status change — this is what actually does that work off the
 * request thread. Owner is passed as type+id rather than the model itself
 * so the job payload stays a small serializable scalar, not a full model
 * (SerializesModels re-fetches it anyway, but this keeps the constructor
 * honest about what's actually needed).
 */
class SendPushNotification implements ShouldQueue
{
    use Queueable;

    /**
     * @param  array<string, string>  $data
     */
    public function __construct(
        private readonly string $ownerType,
        private readonly int $ownerId,
        private readonly string $title,
        private readonly string $body,
        private readonly array $data = [],
    ) {}

    public function handle(FcmService $fcm): void
    {
        $owner = $this->ownerType::find($this->ownerId);

        if (! $owner instanceof User && ! $owner instanceof Customer) {
            return;
        }

        $fcm->sendToOwnerNow($owner, $this->title, $this->body, $this->data);
    }
}
