<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Domain\Canonical;

final readonly class NormalizedListingAddress
{
    public function __construct(
        public string $street,
        public string $number,
        public string $complement,
        public string $postalCode,
        public string $neighborhood,
        public string $city,
        public string $state,
        public ?string $latitude,
        public ?string $longitude,
        public string $country = 'BR',
    ) {}

    public function formatted(): string
    {
        $parts = array_filter([
            $this->street,
            $this->number !== '' ? 'nº '.$this->number : '',
            $this->complement,
            $this->neighborhood,
            $this->city !== '' && $this->state !== '' ? "{$this->city}/{$this->state}" : $this->city.$this->state,
            $this->postalCode !== '' ? 'CEP '.$this->postalCode : '',
        ], static fn (string $part): bool => trim($part) !== '');

        return implode(', ', $parts);
    }
}
