<?php

namespace Modules\Routing\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Catalog\Models\Destination;

class PopularRouteResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'from_destination' => $this->whenLoaded('fromDestination', fn () => self::destinationSummary($this->fromDestination)),
            'to_destination' => $this->whenLoaded('toDestination', fn () => self::destinationSummary($this->toDestination)),
            'description' => $this->description,
            'mm_description' => $this->mm_description,
        ];
    }

    /**
     * Trimmed down from the full DestinationResource (id/name/mm_name only)
     * — a popular route only needs enough to label the two endpoints, not
     * every catalog field.
     *
     * @return array<string, mixed>
     */
    private static function destinationSummary(Destination $destination): array
    {
        return [
            'id' => $destination->id,
            'name' => $destination->name,
            'mm_name' => $destination->mm_name,
        ];
    }
}
