<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\Bpmn;

final class BpmnFlow
{
    public function __construct(
        private readonly string $flowId,
        private readonly string $sourceRef,
        private readonly string $targetRef,
        private readonly string $name = '',
        private readonly string $createdAt = ''
    ) {}

    public static function create(string $flowId, string $sourceRef, string $targetRef, string $name = ''): self
    {
        return new self($flowId, $sourceRef, $targetRef, $name, date('c'));
    }

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

    public function flowId(): string { return $this->flowId; }
    public function sourceRef(): string { return $this->sourceRef; }
    public function targetRef(): string { return $this->targetRef; }

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