package runner

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
)

// ExecutionResult 执行结果
type ExecutionResult struct {
	Output   string `json:"output"`
	Error    string `json:"error"`
	Duration int64  `json:"duration"` // 毫秒
	ExitCode int    `json:"exitCode"`
}

// CodeRunner 代码执行接口
type CodeRunner interface {
	Run(ctx context.Context, code string) (*ExecutionResult, error)
}

// GetRunner 获取对应语言的运行器
func GetRunner(language string) (CodeRunner, error) {
	tempDir := filepath.Join(os.TempDir(), "art-code-runner")
	if err := os.MkdirAll(tempDir, 0755); err != nil {
		return nil, fmt.Errorf("failed to create temp dir: %v", err)
	}

	switch language {
	case "go", "golang":
		return &GoRunner{TempDir: tempDir}, nil
	case "php":
		return &PHPRunner{TempDir: tempDir}, nil
	case "javascript", "js", "node":
		return &NodeRunner{TempDir: tempDir}, nil
	default:
		return nil, fmt.Errorf("unsupported language: %s", language)
	}
}
