<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\SegmentConstant;
use App\Classes\ValueObjects\Constants\SegmentConstants;


class PostcodeResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $centerPostcodeConstantObject = SegmentConstant::where('reference', SegmentConstants::CENTER_POSTCODE)->get();
        $centerPostcodeConstant = $centerPostcodeConstantObject->isEmpty() ? [] : (array)$centerPostcodeConstantObject->first()->value;
        $postcodeArea = in_array($this->resource, $centerPostcodeConstant) ? 'CENTER_POSTCODE' : '';
        
        $outstationPostcodeConstantObject = SegmentConstant::where('reference', SegmentConstants::OUTSTATION_POSTCODE)->get();
        $outstationPostcodeConstant = $outstationPostcodeConstantObject->isEmpty() ? [] : (array)$outstationPostcodeConstantObject->first()->value;
        in_array($this->resource, $outstationPostcodeConstant) ? $postcodeArea = 'OUTSTATION_POSTCODE' : '';

        return [
            'postcode' => $this->resource,
            'postcodeArea' => $postcodeArea,
        ];
    }
}
