<?php

namespace App\Imports\Processors;

use App\Contracts\PendingNotificationProcessor;
use App\Models\NotificationMap;
use App\NotificationMaps\NotificationPayloadMapper;
use App\NotificationMaps\NotificationMapVehicleUpsert;

/**
 * Applies a single captured payload for a configurable notification map: re-maps the
 * payload through the map and upserts the resulting vehicle. Resolved per-map by the
 * shared drain (ProcessPendingProviderNotifications) from the provider key.
 */
class NotificationMapPendingProcessor implements PendingNotificationProcessor
{
    public function __construct(
        private readonly NotificationMap $map,
        private readonly NotificationPayloadMapper $mapper,
        private readonly NotificationMapVehicleUpsert $upsert,
    ) {
    }

    public function process(array $payload): void
    {
        // A map deactivated after capture is a no-op; its queued rows simply drain away.
        if (!$this->map->active) {
            return;
        }

        $mapped = $this->mapper->map($this->map, $payload);
        $this->upsert->upsert($this->map, $mapped);
    }
}
