<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class AddressResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id'             => $this->id,
            'source'         => $this->source,
            'source_id'      => $this->source_id,
            'zip_code'       => $this->zip_code,
            'address'        => $this->address,
            'number'         => $this->number,
            'district'       => $this->district,
            'has_complement' => $this->has_complement,
            'complement'     => $this->complement,
            'nickname'       => $this->nickname,
            'instructions'   => $this->instructions,
            'is_primary'     => $this->is_primary,
            'city_id'        => $this->city_id,
            'state_id'       => $this->state_id,

            'address_full' => implode(', ', array_filter([
                $this->address,
                $this->number ? "nº {$this->number}" : null,
                $this->district,
                $this->city ? "{$this->city->name}/{$this->state?->code}" : null,
            ])),

            'latitude'     => $this->latitude,
            'longitude'    => $this->longitude,
            'city'         => $this->whenLoaded('city'),
            'state'        => $this->whenLoaded('state'),
            'address_type' => $this->address_type,
            'created_at'   => $this->created_at,
            'updated_at'   => $this->updated_at,
            'deleted_at'   => $this->deleted_at,
        ];
    }
}
