<?php

namespace App\Http\Resources;

use App\Classes\ValueObjects\Constants\AddressType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Http\Resources\Json\JsonResource;

class DeliveryOrderResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'reference' => $this->reference,
            'type' => (int) $this->type,
            'status' => (int) $this->status,
            'company_module' => new CompanyModuleResource($this->companyModule),
            'sender_address' => new AddressResource($this->addresses()->where('status', '=', ApprovalStatus::APPROVED)->where('type', AddressType::PICK_UP)->first()),
            'delivery_address' => new AddressResource($this->addresses()->where('status', '=', ApprovalStatus::APPROVED)->where('type', AddressType::DELIVERY)->first()),
            'packingList' => new DeliveryPackingListResource($this->packingLists()->first()),
            'remarks' => RemarkResource::collection($this->remarks),
            'created_at' => $this->created_at->format('d-m-Y')
        ];

    }
}
