package infra

import (
	"fmt"
	"os/exec"
	"strconv"
	"strings"
)

// DockerStopContainer detiene un contenedor Docker. timeoutSecs indica el tiempo de gracia antes de SIGKILL.
func DockerStopContainer(nameOrID string, timeoutSecs int) error {
	args := []string{"stop"}
	if timeoutSecs > 0 {
		args = append(args, "-t", strconv.Itoa(timeoutSecs))
	}
	args = append(args, nameOrID)

	out, err := exec.Command("docker", args...).CombinedOutput()
	if err != nil {
		return fmt.Errorf("docker stop %s: %s", nameOrID, strings.TrimSpace(string(out)))
	}
	return nil
}
