<?php

namespace App\Http\Resources;

use App\Classes\ValueObjects\Constants\ContainerTypes;
use App\Models\Remark;
use Illuminate\Http\Resources\Json\JsonResource;

class ContainerResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'container_reference' => $this->reference,
            'container_specification' => ContainerTypes::CONTAINER_SPECIFICATION[$this->container_type],
            'container_number' => $this->container_number,
            'seal_reference' => $this->seal_reference,
            'loading_date' => $this->loading_date ? $this->loading_date->format('d-m-Y') : $this->loading_date,
            'transport' => new TransportResource($this->transports()->first()),
            'packing_lists' => PackingListResource::collection($this->whenLoaded('packingLists', function(){
                return $this->packingLists()->whereHas('packages')->get();
            })),
            'remarks' => RemarkResource::collection($this->remarks),
            'status' => $this->status
        ];
    }
}
