<?php

namespace App\Http\Resources;

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

class AddressResource 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,
            'street_one' => $this->street_one,
            'street_two' => $this->street_two,
            'district' => $this->district,
            'state' => $this->state,
            'post_code' => $this->postcode,
            'country' => $this->country,
            'default' => (int) $this->default,
            'status' => (int) $this->status,
            'contact' => new ContactResource($this->contacts->first()),
            'remark' => new RemarkResource($this->remarks->first()),
            $this->mergeWhen($this->relationLoaded('owner'), [
                'owner' => $this->when($this->owner instanceof Order, [
                    'reference' => $this->owner->reference,
                    'company_module' => new CompanyModuleResource($this->owner->companyModule),
                    'address' => new AddressResource($this->owner->addresses()->where('status', '=', ApprovalStatus::APPROVED)->first())
                ])
            ])

        ];
    }
}
