//go:build smoke

package smoketest

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

// setupRepoFixture builds the throwaway git repo at runtimeDir/fake-repo
// that every repodir-class scenario consumes. Mirrors
// scripts/smoke.sh:441-456. The path is stored in the package-level
// repoDir so scenarios can reference it directly.
func setupRepoFixture() error {
	repoDir = filepath.Join(runtimeDir, "fake-repo")
	if err := os.MkdirAll(repoDir, 0o755); err != nil {
		return fmt.Errorf("setupRepoFixture: mkdir %s: %w", repoDir, err)
	}
	steps := [][]string{
		{"git", "init", "-q", "-b", "main"},
		{"git", "config", "commit.gpgsign", "false"},
		{"git", "config", "user.name", "smoke"},
		{"git", "config", "user.email", "smoke@smoke"},
	}
	for _, args := range steps {
		cmd := exec.Command(args[0], args[1:]...)
		cmd.Dir = repoDir
		if out, err := cmd.CombinedOutput(); err != nil {
			return fmt.Errorf("setupRepoFixture: %s: %w\n%s", args, err, out)
		}
	}
	marker := filepath.Join(repoDir, "smoke-file.txt")
	if err := os.WriteFile(marker, []byte("smoke-workspace-marker\n"), 0o644); err != nil {
		return fmt.Errorf("setupRepoFixture: write marker: %w", err)
	}
	commit := [][]string{
		{"git", "add", "."},
		{"git", "commit", "-q", "-m", "init"},
	}
	for _, args := range commit {
		cmd := exec.Command(args[0], args[1:]...)
		cmd.Dir = repoDir
		if out, err := cmd.CombinedOutput(); err != nil {
			return fmt.Errorf("setupRepoFixture: %s: %w\n%s", args, err, out)
		}
	}
	return nil
}
