<?php

namespace Modules\Internal\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class ClaimRequestShowResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $data = parent::toArray($request);

        // Map Histories to Group by Dates
        $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
            ];
        }

        // Map Files by type
        $filesGroupByType = $this->files->mapToGroups(function($file) {
            return [Str::slug($file->type, '_') => $file];
        });
        $data['files_by_type'] = $filesGroupByType;

        return $data;
    }
}
