<?php

namespace Modules\HospitalPortal\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class ClaimRequestResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $data = parent::toArray($request);
        $historiesGroupByDate = $this->histories->mapToGroups(function($history) {
            return [$history->created_at->format('Y-m-d') => $history];
        });
        $data['histories_by_date'] = [];
        foreach ($historiesGroupByDate as $date => $histories) {
            $data['histories_by_date'][] = [
                'date' => $date,
                'histories' => $histories
            ];
        }

        return $data; //parent::toArray($request);
    }
}
