<?php

namespace App\Http\Resources;

use App\Models\NotificationTemplate;
use App\NotificationMaps\NotificationPayloadMapper;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Mtc\MercuryDataModels\Vehicle;

class NotificationMapResource extends JsonResource
{
    public static $wrap = '';

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    #[\Override]
    public function toArray(Request $request): array
    {
        $data = parent::toArray($request);

        $data['inbound_url'] = $this->resource->getInboundUrl();
        $data['docs_url'] = $this->resource->getDocsUrl();
        $data['data_options'] = [
            'available_fields' => collect((new Vehicle())->getFillable())
                ->map(fn ($column) => ['id' => $column, 'name' => $column])
                ->sortBy('name')
                ->values(),
            'field_types' => collect(NotificationPayloadMapper::TYPES)
                ->map(fn ($type) => ['id' => $type, 'name' => $type])
                ->values(),
            // Published system templates of the same type, offered to apply to the map.
            // Full records so the admin UI can copy field_map/validation/config client-side
            // (tenant admins cannot reach the superadmin-only template endpoint).
            'templates' => NotificationTemplate::query()
                ->published()
                ->where('type', $this->resource->type)
                ->get(),
        ];

        return $data;
    }
}
