<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Streaming\Replay;

final class StreamPersistedEvent
{
    public function __construct(private readonly int $sequence, private readonly string $eventId, private readonly string $topic, private readonly string $type, private readonly array $payload = [], private readonly array $headers = [], private readonly string $messageId = '', private readonly string $persistedAt = '') {}
    public static function create(int $sequence, array $message): self { return new self($sequence, 'event_' . str_pad((string) $sequence, 8, '0', STR_PAD_LEFT), (string) ($message['topic'] ?? ''), (string) ($message['type'] ?? ''), (array) ($message['payload'] ?? []), (array) ($message['headers'] ?? []), (string) ($message['messageId'] ?? ''), date('c')); }
    public static function fromArray(array $data): self { return new self((int) ($data['sequence'] ?? 0), (string) ($data['eventId'] ?? ''), (string) ($data['topic'] ?? ''), (string) ($data['type'] ?? ''), (array) ($data['payload'] ?? []), (array) ($data['headers'] ?? []), (string) ($data['messageId'] ?? ''), (string) ($data['persistedAt'] ?? '')); }
    public function sequence(): int { return $this->sequence; }
    public function eventId(): string { return $this->eventId; }
    public function topic(): string { return $this->topic; }
    public function toArray(): array { return ['sequence'=>$this->sequence,'eventId'=>$this->eventId,'topic'=>$this->topic,'type'=>$this->type,'payload'=>$this->payload,'headers'=>$this->headers,'messageId'=>$this->messageId,'persistedAt'=>$this->persistedAt !== '' ? $this->persistedAt : date('c')]; }
}
