<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Domain\Canonical;

/**
 * Representação canônica de um imóvel — independente de portal ou formato de exportação.
 *
 * Todo conector (XML, API de portal, catálogo) deve consumir este objeto,
 * nunca o modelo de persistência diretamente.
 */
final readonly class NormalizedListing
{
    /**
     * @param  list<NormalizedListingPhoto>  $photos
     * @param  list<NormalizedListingCharacteristic>  $characteristics
     */
    public function __construct(
        public string $code,
        public string $title,
        public string $description,
        public ?string $price,
        public string $type,
        public string $purpose,
        public string $status,
        public NormalizedListingAddress $address,
        public array $photos,
        public array $characteristics,
        public ?NormalizedListingBroker $broker,
        public ?NormalizedListingCompany $company,
        public ?string $detailUrl = null,
        public ?string $propertyId = null,
        public string $currency = 'BRL',
    ) {}

    public function neighborhood(): string
    {
        return $this->address->neighborhood;
    }

    public function city(): string
    {
        return $this->address->city;
    }

    public function state(): string
    {
        return $this->address->state;
    }

    public function latitude(): ?string
    {
        return $this->address->latitude;
    }

    public function longitude(): ?string
    {
        return $this->address->longitude;
    }

    public function addressLine(): string
    {
        return $this->address->formatted();
    }

    /** @return list<string> */
    public function exportValidationErrors(): array
    {
        $errors = [];

        if (trim($this->title) === '') {
            $errors[] = 'O imóvel precisa de título para publicar.';
        }

        if ($this->price === null || $this->price === '' || (float) $this->price <= 0) {
            $errors[] = 'O imóvel precisa de preço válido para publicar.';
        }

        return $errors;
    }

    public function isValidForExport(): bool
    {
        return $this->exportValidationErrors() === [];
    }
}
