<?php

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

class StateResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        $segmentConstantObject = SegmentConstant::where('reference', SegmentConstants::STATE_RATE)->get();
        $segmentConstant = $segmentConstantObject->isEmpty() ? [] : (array)$segmentConstantObject->first()->value;

        return [
            'id' => $this->id,
            'name' => $this->name,
            'country' => $this->country,
            'stateCharges' => array_key_exists($this->id, $segmentConstant) ? $segmentConstant[$this->id] : null,
        ];
    }
}
