<?php

declare(strict_types=1);

namespace App\Framework\Console;

/**
 * Result of workflow execution
 */
final readonly class WorkflowResult
{
    public function __construct(
        private bool $success,
        private array $stepResults = [],
        private string $error = '',
        private array $metadata = []
    ) {
    }

    /**
     * Create success result
     */
    public static function success(array $stepResults = [], array $metadata = []): self
    {
        return new self(true, $stepResults, '', $metadata);
    }

    /**
     * Create error result
     */
    public static function error(string $error, array $stepResults = [], array $metadata = []): self
    {
        return new self(false, $stepResults, $error, $metadata);
    }

    /**
     * Check if workflow was successful
     */
    public function isSuccess(): bool
    {
        return $this->success;
    }

    /**
     * Check if workflow failed
     */
    public function isError(): bool
    {
        return ! $this->success;
    }

    /**
     * Get step results
     */
    public function getStepResults(): array
    {
        return $this->stepResults;
    }

    /**
     * Get error message
     */
    public function getError(): string
    {
        return $this->error;
    }

    /**
     * Get metadata
     */
    public function getMetadata(): array
    {
        return $this->metadata;
    }

    /**
     * Get successful steps count
     */
    public function getSuccessfulStepsCount(): int
    {
        return count(array_filter($this->stepResults, fn (StepResult $result) => $result->isSuccess()));
    }

    /**
     * Get failed steps count
     */
    public function getFailedStepsCount(): int
    {
        return count(array_filter($this->stepResults, fn (StepResult $result) => $result->isError()));
    }

    /**
     * Get total steps count
     */
    public function getTotalStepsCount(): int
    {
        return count($this->stepResults);
    }
}
