<?php

declare(strict_types=1);

namespace App\Framework\Console;

/**
 * Represents a single step in a command workflow
 */
final readonly class WorkflowStep
{
    public function __construct(
        public string $command,
        public array $parameters = [],
        public string $description = '',
        public bool $optional = false,
        public int $retryCount = 0,
        public array $environment = [],
        public ?string $condition = null
    ) {
    }

    /**
     * Create step from array
     */
    public static function fromArray(array $data): self
    {
        return new self(
            command: $data['command'] ?? '',
            parameters: $data['parameters'] ?? [],
            description: $data['description'] ?? '',
            optional: $data['optional'] ?? false,
            retryCount: $data['retryCount'] ?? 0,
            environment: $data['environment'] ?? [],
            condition: $data['condition'] ?? null
        );
    }

    /**
     * Convert to array
     */
    public function toArray(): array
    {
        return [
            'command' => $this->command,
            'parameters' => $this->parameters,
            'description' => $this->description,
            'optional' => $this->optional,
            'retryCount' => $this->retryCount,
            'environment' => $this->environment,
            'condition' => $this->condition,
        ];
    }

    /**
     * Get command name
     */
    public function getCommand(): string
    {
        return $this->command;
    }

    /**
     * Get command parameters
     */
    public function getParameters(): array
    {
        return $this->parameters;
    }

    /**
     * Get step description
     */
    public function getDescription(): string
    {
        return $this->description;
    }

    /**
     * Check if step is optional
     */
    public function isOptional(): bool
    {
        return $this->optional;
    }

    /**
     * Get retry count
     */
    public function getRetryCount(): int
    {
        return $this->retryCount;
    }

    /**
     * Get environment variables
     */
    public function getEnvironment(): array
    {
        return $this->environment;
    }

    /**
     * Get condition for execution
     */
    public function getCondition(): ?string
    {
        return $this->condition;
    }

    /**
     * Check if step should be executed based on condition
     */
    public function shouldExecute(array $context = []): bool
    {
        if ($this->condition === null) {
            return true;
        }

        // Simple condition evaluation - can be extended
        return match($this->condition) {
            'always' => true,
            'never' => false,
            'development' => ($context['environment'] ?? '') === 'development',
            'production' => ($context['environment'] ?? '') === 'production',
            default => true
        };
    }
}
