package workflow

import (
	"fmt"
	"os"
)

// Config holds workflow engine configuration
type Config struct {
	// MaxRetries is the maximum number of retries for a failed node
	MaxRetries int
	// RetryDelay is the delay between retries in seconds
	RetryDelay int
}

// DefaultConfig returns the default workflow configuration
func DefaultConfig() *Config {
	return &Config{
		MaxRetries: 3,
		RetryDelay: 60,
	}
}

// LoadConfig loads the workflow configuration from environment variables
func LoadConfig() *Config {
	config := DefaultConfig()

	// Override with environment variables if set
	if maxRetries := os.Getenv("WORKFLOW_MAX_RETRIES"); maxRetries != "" {
		config.MaxRetries = parseInt(maxRetries, config.MaxRetries)
	}

	if retryDelay := os.Getenv("WORKFLOW_RETRY_DELAY"); retryDelay != "" {
		config.RetryDelay = parseInt(retryDelay, config.RetryDelay)
	}

	return config
}

// parseInt parses a string to int, returning default if parsing fails
func parseInt(s string, defaultValue int) int {
	var result int
	_, err := fmt.Sscanf(s, "%d", &result)
	if err != nil {
		return defaultValue
	}
	return result
}
