package infra

import (
	"encoding/json"
	"fmt"
	"os/exec"
	"strings"
)

// DockerVolumeList lista los volumes Docker disponibles localmente.
// Retorna un slice de maps con campos Driver, Name, Scope, Labels, Mountpoint.
func DockerVolumeList() ([]map[string]string, error) {
	out, err := exec.Command("docker", "volume", "ls", "--format", "{{json .}}").Output()
	if err != nil {
		return nil, fmt.Errorf("docker volume ls: %w", err)
	}

	if len(strings.TrimSpace(string(out))) == 0 {
		return nil, nil
	}

	var volumes []map[string]string
	for _, line := range splitLines(out) {
		if len(line) == 0 {
			continue
		}
		var raw map[string]string
		if err := json.Unmarshal(line, &raw); err != nil {
			continue
		}
		volumes = append(volumes, raw)
	}

	return volumes, nil
}
