<?php

namespace App\Http\Resources;

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

class ReviewResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'          => $this->id,
            'schedule_id' => $this->schedule_id,
            'schedule_label' => $this->schedule
                ? ($this->schedule->id . ' - ' .
                   ($this->schedule->client?->user?->name ?? '?') . ' - ' .
                   ($this->schedule->provider?->user?->name ?? '?') . ' - ' .
                   ($this->schedule->date?->format('d/m/Y') ?? '?'))
                : null,
            'origin'      => $this->origin,
            'origin_id'   => $this->origin_id,
            'stars'       => $this->stars,
            'comment'     => $this->comment,
            'reviews_improvements' => $this->whenLoaded('reviewsImprovements', function () {
                return $this->reviewsImprovements->map(function ($type) {
                    return [
                        'id' => $type->improvementType->id,
                        'description' => $type->improvementType->description,
                    ];
                });
            }),
            'created_at'  => $this->created_at?->format('Y-m-d H:i'),
            'updated_at'  => $this->updated_at?->format('Y-m-d H:i'),
        ];
    }
}
