<?php

namespace App\Http\Resources;

use App\Classes\Constants;
use App\Models\Schedule as ScheduleModel;
use App\Models\Contact as ContactModel;
use App\Models\TrackingNumber;
use Illuminate\Http\Resources\Json\JsonResource;

class DeliveryArrangement extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'type' => (int)$this->type,
            'vehicle_no' => $this->vehicle_no,
            'schedule' => Schedule::collection(ScheduleModel::where('arrangement_id', $this->id)->where('type', 1)->orderBy('created_at', 'desc')->get()),
            'tracking' => TrackingNumber::where('order_id', $this->order_id)->where('type', 1)->first(),
            'contact' => new Contact(ContactModel::where('type', Constants::MODULE_TYPES['driver'])->where('reference_id', $this->id)->first())
        ];
    }
}
