package handler

import (
	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/mem"
	"x-agents/server/internal/model"
)

func getCPUPercent() (float64, error) {
	percent, err := cpu.Percent(0, false)
	if err != nil {
		return 0, err
	}
	if len(percent) > 0 {
		return percent[0], nil
	}
	return 0, nil
}

func getCPUCoreCount() (int, error) {
	count, err := cpu.Counts(false)
	if err != nil {
		return 0, err
	}
	return count, nil
}

func getCPUModelName() (string, error) {
	info, err := cpu.Info()
	if err != nil {
		return "Unknown", err
	}
	if len(info) > 0 {
		return info[0].ModelName, nil
	}
	return "Unknown", nil
}

func getMemoryInfo() (*model.MemoryInfo, error) {
	v, err := mem.VirtualMemory()
	if err != nil {
		return nil, err
	}

	// 计算使用率
	percent := 0.0
	if v.Total > 0 {
		percent = float64(v.Used) / float64(v.Total) * 100
	}

	return &model.MemoryInfo{
		Total:       v.Total,
		Used:        v.Used,
		Available:   v.Available,
		Percent:     percent,
		TotalGB:     float64(v.Total) / 1024 / 1024 / 1024,
		UsedGB:      float64(v.Used) / 1024 / 1024 / 1024,
		AvailableGB: float64(v.Available) / 1024 / 1024 / 1024,
	}, nil
}
