<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Application\Data;

use Carbon\CarbonInterface;
use ImovelZapAi\Publishing\Domain\Enums\TenantFeedStatus;

final readonly class PublicationCenterFeedData
{
    /**
     * @param  list<string>  $errors
     */
    public function __construct(
        public string $groupName,
        public string $feedUrl,
        public TenantFeedStatus $status,
        public int $listingCount,
        public ?CarbonInterface $lastReadAt,
        public ?CarbonInterface $lastGeneratedAt,
        public array $errors,
    ) {}

    public function statusLabel(): string
    {
        return $this->status->label();
    }

    public function statusTone(): string
    {
        return $this->status->tone();
    }

    public function lastReadAtLabel(): ?string
    {
        return $this->formatTimestamp($this->lastReadAt);
    }

    public function lastGeneratedAtLabel(): ?string
    {
        return $this->formatTimestamp($this->lastGeneratedAt);
    }

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

    public function errorsSummary(): ?string
    {
        if ($this->errors === []) {
            return null;
        }

        return $this->errors[0];
    }

    private function formatTimestamp(?CarbonInterface $value): ?string
    {
        return $value?->timezone(config('app.timezone'))->format('d/m/Y H:i');
    }
}
