<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Connectors\GroupOlx\Application\Data;

use Carbon\CarbonInterface;

/**
 * Painel Publishing — Grupo OLX (VRSync). Sem gráficos.
 */
final readonly class GroupOlxDashboardData
{
    /**
     * @param  list<string>  $errors
     */
    public function __construct(
        public bool $xmlValid,
        public ?CarbonInterface $lastGeneratedAt,
        public int $listingCount,
        public array $errors,
        public string $publicUrl,
        public ?int $xmlSizeBytes,
        public ?int $generationDurationMs,
        public bool $available,
        public ?string $storageVersion = null,
        public ?string $contentHash = null,
    ) {}

    public function xmlValidLabel(): string
    {
        if (! $this->available) {
            return 'Indisponível';
        }

        return $this->xmlValid ? 'Válido' : 'Inválido';
    }

    public function lastGeneratedAtLabel(): ?string
    {
        return $this->lastGeneratedAt
            ?->timezone(config('app.timezone'))
            ->format('d/m/Y H:i');
    }

    public function xmlSizeLabel(): string
    {
        if ($this->xmlSizeBytes === null) {
            return '—';
        }

        $bytes = $this->xmlSizeBytes;

        if ($bytes < 1024) {
            return $bytes.' B';
        }

        if ($bytes < 1024 * 1024) {
            return number_format($bytes / 1024, 1, ',', '.').' KB';
        }

        return number_format($bytes / (1024 * 1024), 2, ',', '.').' MB';
    }

    public function generationDurationLabel(): string
    {
        if ($this->generationDurationMs === null) {
            return '—';
        }

        if ($this->generationDurationMs < 1000) {
            return $this->generationDurationMs.' ms';
        }

        return number_format($this->generationDurationMs / 1000, 2, ',', '.').' s';
    }

    public function hasErrors(): bool
    {
        return $this->errors !== [];
    }
}
