//go:build darwin

package stats

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

func (r *Reporter) memoryStatus() (total, avail uint64) {
	out, err := exec.Command("sysctl", "-n", "hw.memsize").Output()
	if err != nil {
		return 0, 0
	}
	total, _ = strconv.ParseUint(strings.TrimSpace(string(out)), 10, 64)
	out, err = exec.Command("vm_stat").Output()
	if err != nil {
		return total, total / 2
	}
	// Rough available estimate from vm_stat free pages
	var pageSize uint64 = 4096
	var freePages uint64
	for _, line := range strings.Split(string(out), "\n") {
		if strings.Contains(line, "Pages free") {
			parts := strings.Fields(line)
			if len(parts) >= 3 {
				freePages, _ = strconv.ParseUint(strings.Trim(parts[2], "."), 10, 64)
			}
		}
	}
	avail = freePages * pageSize
	return total, avail
}
