package system

import (
	"fmt"
	"runtime"

	"u-desk/internal/common"

	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/host"
	"github.com/shirou/gopsutil/v3/mem"
)

// GetSystemInfo 获取系统基本信息
func GetSystemInfo() (map[string]interface{}, error) {
	hostInfo, err := host.Info()
	if err != nil {
		return nil, fmt.Errorf("获取主机信息失败: %v", err)
	}

	return map[string]interface{}{
		"os":           runtime.GOOS,
		"arch":         runtime.GOARCH,
		"hostname":     hostInfo.Hostname,
		"platform":     hostInfo.Platform,
		"platform_ver": hostInfo.PlatformVersion,
		"kernel_ver":   hostInfo.KernelVersion,
		"uptime":       hostInfo.Uptime,
		"boot_time":    hostInfo.BootTime,
		"cpu_count":    runtime.NumCPU(),
	}, nil
}

// GetCPUInfo 获取 CPU 信息
func GetCPUInfo() (map[string]interface{}, error) {
	cpuCount, err := cpu.Counts(true)
	if err != nil {
		return nil, fmt.Errorf("获取 CPU 核心数失败: %v", err)
	}

	cpuPercent, err := cpu.Percent(0, false)
	if err != nil {
		return nil, fmt.Errorf("获取 CPU 使用率失败: %v", err)
	}

	cpuInfo, err := cpu.Info()
	if err != nil {
		return nil, fmt.Errorf("获取 CPU 详细信息失败: %v", err)
	}

	cpuModel := "Unknown"
	if len(cpuInfo) > 0 {
		cpuModel = cpuInfo[0].ModelName
	}

	usage := 0.0
	if len(cpuPercent) > 0 {
		usage = cpuPercent[0]
	}

	return map[string]interface{}{
		"cores":     cpuCount,
		"model":     cpuModel,
		"usage":     fmt.Sprintf("%.2f%%", usage),
		"usage_raw": usage,
	}, nil
}

// GetMemoryInfo 获取内存信息
func GetMemoryInfo() (map[string]interface{}, error) {
	memInfo, err := mem.VirtualMemory()
	if err != nil {
		return nil, fmt.Errorf("获取内存信息失败: %v", err)
	}

	return map[string]interface{}{
		"total":         memInfo.Total,
		"used":          memInfo.Used,
		"free":          memInfo.Free,
		"available":     memInfo.Available,
		"usage":         fmt.Sprintf("%.2f%%", memInfo.UsedPercent),
		"usage_raw":     memInfo.UsedPercent,
		"total_str":     common.FormatBytes(memInfo.Total),
		"used_str":      common.FormatBytes(memInfo.Used),
		"free_str":      common.FormatBytes(memInfo.Free),
		"available_str": common.FormatBytes(memInfo.Available),
	}, nil
}

// GetDiskInfo 获取磁盘信息
func GetDiskInfo() ([]map[string]interface{}, error) {
	partitions, err := disk.Partitions(false)
	if err != nil {
		return nil, fmt.Errorf("获取磁盘分区失败: %v", err)
	}

	var result []map[string]interface{}

	for _, partition := range partitions {
		usage, err := disk.Usage(partition.Mountpoint)
		if err != nil {
			continue
		}

		result = append(result, map[string]interface{}{
			"device":     partition.Device,
			"mountpoint": partition.Mountpoint,
			"fstype":     partition.Fstype,
			"total":      usage.Total,
			"used":       usage.Used,
			"free":       usage.Free,
			"usage":      fmt.Sprintf("%.2f%%", usage.UsedPercent),
			"usage_raw":  usage.UsedPercent,
			"total_str":  common.FormatBytes(usage.Total),
			"used_str":   common.FormatBytes(usage.Used),
			"free_str":   common.FormatBytes(usage.Free),
		})
	}

	return result, nil
}
