<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\Bpmn;

final class BpmnElement
{
    public function __construct(
        private readonly string $elementId,
        private readonly string $type,
        private readonly string $name = '',
        private readonly array $attributes = [],
        private readonly string $createdAt = ''
    ) {}

    public static function create(string $elementId, string $type, string $name = '', array $attributes = []): self
    {
        return new self($elementId, $type, $name, $attributes, date('c'));
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['elementId'] ?? ''),
            (string) ($data['type'] ?? ''),
            (string) ($data['name'] ?? ''),
            (array) ($data['attributes'] ?? []),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function elementId(): string { return $this->elementId; }
    public function type(): string { return $this->type; }
    public function name(): string { return $this->name; }

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