<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\Engine;

final class WorkflowEngineDefinition
{
    public function __construct(
        private readonly string $workflowId,
        private readonly string $name,
        private readonly array $activities = [],
        private readonly array $gateways = [],
        private readonly array $transitions = [],
        private readonly string $startActivityId = '',
        private readonly string $status = 'active',
        private readonly string $createdAt = ''
    ) {}

    public static function create(string $workflowId, string $name, array $activities, array $gateways, array $transitions, string $startActivityId): self
    {
        return new self($workflowId, $name, $activities, $gateways, $transitions, $startActivityId, 'active', date('c'));
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['workflowId'] ?? ''),
            (string) ($data['name'] ?? ''),
            (array) ($data['activities'] ?? []),
            (array) ($data['gateways'] ?? []),
            (array) ($data['transitions'] ?? []),
            (string) ($data['startActivityId'] ?? ''),
            (string) ($data['status'] ?? 'active'),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function workflowId(): string { return $this->workflowId; }
    public function startActivityId(): string { return $this->startActivityId; }
    public function active(): bool { return $this->status === 'active'; }
    public function activities(): array { return $this->activities; }
    public function gateways(): array { return $this->gateways; }

    public function gateway(string $gatewayId): ?WorkflowGateway
    {
        foreach ($this->gateways as $gateway) {
            if ((string) ($gateway['gatewayId'] ?? '') === $gatewayId) {
                return WorkflowGateway::fromArray((array) $gateway);
            }
        }
        return null;
    }

    public function next(string $from, array $context = []): array
    {
        $next = [];
        foreach ($this->transitions as $transition) {
            if ((string) ($transition['from'] ?? '') !== $from) {
                continue;
            }

            $conditionKey = (string) ($transition['conditionKey'] ?? '');
            $conditionValue = $transition['conditionValue'] ?? null;
            if ($conditionKey !== '' && (($context[$conditionKey] ?? null) !== $conditionValue)) {
                continue;
            }

            $to = (string) ($transition['to'] ?? '');
            if ($to !== '') {
                $next[] = $to;
            }
        }

        return $next;
    }

    public function toArray(): array
    {
        return [
            'workflowId' => $this->workflowId,
            'name' => $this->name,
            'activities' => $this->activities,
            'gateways' => $this->gateways,
            'transitions' => $this->transitions,
            'startActivityId' => $this->startActivityId,
            'status' => $this->status,
            'active' => $this->active(),
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
        ];
    }
}
