package handler

import (
	"net/http"

	"x-agents/server/internal/model"

	"github.com/gin-gonic/gin"
)

type SystemHandler struct{}

func NewSystemHandler() *SystemHandler {
	return &SystemHandler{}
}

// GetSystemInfo 获取系统信息
func (h *SystemHandler) GetSystemInfo(c *gin.Context) {
	info, err := getSystemInfo()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, info)
}

// getSystemInfo 获取系统信息
func getSystemInfo() (*model.SystemInfo, error) {
	// 获取CPU使用率
	cpuPercent, err := getCPUPercent()
	if err != nil {
		return nil, err
	}

	// 获取CPU核心数
	coreCount, err := getCPUCoreCount()
	if err != nil {
		coreCount = 0
	}

	// 获取CPU型号
	modelName, err := getCPUModelName()
	if err != nil {
		modelName = "Unknown"
	}

	// 获取内存信息
	memoryInfo, err := getMemoryInfo()
	if err != nil {
		return nil, err
	}

	return &model.SystemInfo{
		CPU: model.CPUInfo{
			Percent:   cpuPercent,
			CoreCount: coreCount,
			ModelName: modelName,
		},
		Memory: *memoryInfo,
	}, nil
}
