<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow;

final class WorkflowActivity
{
    public function __construct(
        private readonly string $activityId,
        private readonly string $name,
        private readonly string $type = 'task',
        private readonly array $config = [],
        private readonly string $createdAt = ''
    ) {}

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

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

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

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