package workspace

import (
	"cursor-api-proxy/internal/config"
	"os"
	"path/filepath"
	"strings"
)

type WorkspaceResult struct {
	WorkspaceDir string
	TempDir      string
}

func ResolveWorkspace(cfg config.BridgeConfig, workspaceHeader string) WorkspaceResult {
	if cfg.ChatOnlyWorkspace {
		tempDir, err := os.MkdirTemp("", "cursor-proxy-")
		if err != nil {
			tempDir = filepath.Join(os.TempDir(), "cursor-proxy-fallback")
			_ = os.MkdirAll(tempDir, 0700)
		}
		return WorkspaceResult{WorkspaceDir: tempDir, TempDir: tempDir}
	}

	headerWs := strings.TrimSpace(workspaceHeader)
	if headerWs != "" {
		return WorkspaceResult{WorkspaceDir: headerWs}
	}
	return WorkspaceResult{WorkspaceDir: cfg.Workspace}
}
