<?php

namespace Modules\Internal\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
use App\Models\Organization;

class RequestLogResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $filesGroupByType = $this->files->mapToGroups(function($file) {
            return [Str::slug($file->type, '_') => $file];
        });
        $provider = Organization::where('id', $this->organization_id)->first();
        
        $data = [
            'id' => $this->id,
            'code' => $this->code,
            'submission_date' => $this->submission_date,
            'member_name' => $this->member->name,
            'status' => $this->status ?? 'unknown',
            'provider' => $provider ? $provider->name : '-',
            'status_final_log' => $this->status_final_log ?? 'unknown',
            'service_name' => $this->service ? $this->service->name : '',
            'payment_type' => $this->payment_type,
            'payment_type_name' => $this->payment_type_name,
            'files_by_type' => $filesGroupByType
        ];

        return $data;
    }
}
