<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

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

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    #[\Override]
    public function toArray($request): array
    {
        $this->resource->loadMissing('mediaUses.media');
        $image = $this->resource->mediaUses
            ->sortByDesc('primary')
            ->first();

        return array_merge(parent::toArray($request), [
            'image' => $image ? [
                'src' => $image->getUrl('square-tile'),
                'alt' => $image->alt_text,
                'original' => $image->media?->getOriginalUrlAttribute(),
            ] : null,
        ]);
    }
}
