<?php
declare(strict_types=1);

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

final class StreamDlqResult
{
    public function __construct(
        private readonly string $action,
        private readonly bool $successful = true,
        private readonly int $affected = 0,
        private readonly string $message = '',
        private readonly array $items = [],
        private readonly string $createdAt = ''
    ) {}

    public static function create(string $action, bool $successful, int $affected, string $message = '', array $items = []): self
    {
        return new self($action, $successful, $affected, $message, $items, date('c'));
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['action'] ?? ''),
            (bool) ($data['successful'] ?? false),
            (int) ($data['affected'] ?? 0),
            (string) ($data['message'] ?? ''),
            (array) ($data['items'] ?? []),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function successful(): bool { return $this->successful; }
    public function affected(): int { return $this->affected; }

    public function toArray(): array
    {
        return [
            'action' => $this->action,
            'successful' => $this->successful,
            'affected' => $this->affected,
            'message' => $this->message,
            'items' => $this->items,
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
        ];
    }
}
