package system

import (
	"fmt"
	"runtime"

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

type Info struct {
	Hostname   string
	OS         string
	Kernel     string
	Platform   string
	Arch       string
	CPUModel   string
	Cores      int
	Threads    int
	TotalRAM   uint64
	DiskTotal  uint64
	Uptime     uint64
}

func Collect() (*Info, error) {
	h, _ := host.Info()
	vm, _ := mem.VirtualMemory()
	c, _ := cpu.Info()
	count, _ := cpu.Counts(false)
	threads, _ := cpu.Counts(true)
	d, _ := disk.Usage("/")

	model := ""
	if len(c) > 0 {
		model = c[0].ModelName
	}

	return &Info{
		Hostname:  h.Hostname,
		OS:        h.OS,
		Kernel:    h.KernelVersion,
		Platform:  h.Platform,
		Arch:      runtime.GOARCH,
		CPUModel:  model,
		Cores:     count,
		Threads:   threads,
		TotalRAM:  vm.Total,
		DiskTotal: d.Total,
		Uptime:    h.Uptime,
	}, nil
}

func (i *Info) Pretty() string {
	return fmt.Sprintf(
`Hostname:    %s
OS:          %s (%s)
Kernel:      %s
Architecture:%s

CPU:         %s
Cores:       %d
Threads:     %d

Total RAM:   %.2f GB
Disk Total:  %.2f GB
Uptime:      %.2f hrs`,
		i.Hostname,
		i.OS, i.Platform,
		i.Kernel,
		i.Arch,
		i.CPUModel,
		i.Cores,
		i.Threads,
		float64(i.TotalRAM)/1e9,
		float64(i.DiskTotal)/1e9,
		float64(i.Uptime)/3600,
	)
}

