<?php

namespace App\Http\Resources\Messaging;

use Illuminate\Http\Resources\Json\ResourceCollection;

class MessageCollection extends ResourceCollection
{
    public function with($request): array
    {
        if (!method_exists($this->resource, 'total')) {
            return [];
        }

        return [
            'meta' => [
                'total' => $this->resource->total() ?? null,
                'per_page' => $this->resource->perPage() ?? null,
                'current_page' => $this->resource->currentPage() ?? null,
                'last_page' => $this->resource->lastPage() ?? null,
            ],
        ];
    }

    public function toArray($request): array
    {
        return $this->collection->map(function ($item) use ($request) {
            return (new MessageResource($item))->toArray($request);
        })->all();
    }
}
