<?php

namespace App\Http\Resources;

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

class ActivityCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @return array<int|string, mixed>
     */
    public function toArray(Request $request): array
    {
        // Transform data to add user_name attribute
        $this->collection->transform(function ($activity) {
            $activity->setAttribute('user_name', optional($activity->user)->name);

            return $activity;
        });

        return $this->resource->toArray();
    }
}
