<?php

namespace App\Http\Resources;

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

class DispatchGuideResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'          => $this->id,
            'date'        => $this->date,
            'n_document'  => $this->n_document,
            'idproduct'   => $this->idproduct,
            'quantity'    => $this->quantity,
            'idwarehouse' => $this->idwarehouse,
            'warehouse'     => $this->whenLoaded('warehouse', function () {
                return [
                    'id' => $this->warehouse->id,
                    'name' => $this->warehouse->name,
                ];
            }),
            'status'      => $this->status,
            'product'     => $this->whenLoaded('product', function () {
                return [
                    'name' => $this->product->name,
                    'sku'  => $this->product->sku,
                ];
            }),
            'details'     => DispatchGuideDetailResource::collection($this->whenLoaded('details')),
        ];
    }
}
