package workspace

import (
	"github.com/moby/moby/api/types/mount"
	"github.com/schmitthub/clawker/internal/config"
	"github.com/schmitthub/clawker/internal/logger"
)

// GitCredentialSetupResult holds the mounts and environment variables
// needed for git credential forwarding.
type GitCredentialSetupResult struct {
	Mounts []mount.Mount
	Env    []string
}

// SetupGitCredentials configures mounts and environment variables for git credential forwarding.
// It returns the mounts to add and environment variables to set based on the config and
// whether the host proxy is running.
//
// SSH and GPG agent forwarding are handled by the socketbridge package (via docker exec),
// not by host proxy or socket mounts. Only HTTPS credentials and git config are handled here.
func SetupGitCredentials(cfg *config.GitCredentialsConfig, hostProxyRunning bool, log *logger.Logger) GitCredentialSetupResult {
	var result GitCredentialSetupResult

	// HTTPS credential forwarding (requires host proxy)
	if cfg.GitHTTPSEnabled(hostProxyRunning) {
		result.Env = append(result.Env, "CLAWKER_GIT_HTTPS=true")
		log.Debug().Msg("git HTTPS credential forwarding enabled")
	}

	// Git config forwarding
	if cfg.CopyGitConfigEnabled() {
		if GitConfigExists() {
			result.Mounts = append(result.Mounts, GetGitConfigMount(log)...)
			log.Debug().Msg("host gitconfig mount enabled")
		}
	}

	return result
}
