<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Streaming;

final class MessageBusTopic
{
    public const ACTIVE = 'active';
    public const PAUSED = 'paused';

    public function __construct(
        private readonly string $name,
        private readonly string $status = self::ACTIVE,
        private readonly int $priority = 0,
        private readonly string $createdAt = ''
    ) {}

    public static function register(string $name, int $priority = 0): self
    {
        return new self($name, self::ACTIVE, $priority, date('c'));
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['name'] ?? ''),
            (string) ($data['status'] ?? self::ACTIVE),
            (int) ($data['priority'] ?? 0),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function name(): string { return $this->name; }
    public function active(): bool { return $this->status === self::ACTIVE; }

    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'status' => $this->status,
            'active' => $this->active(),
            'priority' => $this->priority,
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
        ];
    }
}
