<?php

namespace App\Http\Resources;

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

class RouteResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'            => $this->id,
            'date'          => $this->date,
            'n_route'       => $this->n_route,
            'patente'       => $this->patente,
            'driver'        => $this->driver,
            'observations'  => $this->observations,
            'status'        => $this->status,
            'idcompany'     => $this->idcompany,
            'user'          => $this->whenLoaded('user', function () {
                return [
                    'name'  => $this->user->name,
                    'email' => $this->user->email,
                ];
            }),
            'company'       => $this->whenLoaded('company', function () {
                return [
                    'name' => $this->company->business_name,
                    'rut'  => $this->company->rut,
                ];
            }),
            'dispatch_guides' => DispatchGuideResource::collection($this->whenLoaded('dispatchGuides')),
        ];
    }
}
