<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Queue;

final class QueueSnapshot
{
    public function __construct(
        private readonly string $driver,
        private readonly array $events,
        private readonly string $createdAt = ''
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['driver'] ?? 'unknown'),
            (array) ($data['events'] ?? []),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function driver(): string { return $this->driver; }
    public function events(): array { return $this->events; }
    public function createdAt(): string { return $this->createdAt !== '' ? $this->createdAt : date('c'); }

    public function toArray(): array
    {
        return [
            'driver' => $this->driver,
            'events' => $this->events,
            'createdAt' => $this->createdAt(),
            'count' => count($this->events),
        ];
    }
}
