package facts

import (
	"encoding/json"
	"io/fs"
	"os"
	"path/filepath"
)

// Snapshot mirrors sentinel output; kept minimal to avoid tight coupling.
type Snapshot struct {
	Hostname      string            `json:"hostname,omitempty"`
	Kernel        string            `json:"kernel,omitempty"`
	OSImage       string            `json:"os_image,omitempty"`
	K3sVersion    string            `json:"k3s_version,omitempty"`
	Containerd    string            `json:"containerd,omitempty"`
	PackageSample map[string]string `json:"package_sample,omitempty"`
	DropInsSample map[string]string `json:"dropins_sample,omitempty"`
	USBScratch    *USBScratch       `json:"usb_scratch,omitempty"`
}

// LoadDir reads all *.json under a directory and returns snapshots.
func LoadDir(dir string) ([]Snapshot, error) {
	var snaps []Snapshot
	err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() || filepath.Ext(path) != ".json" {
			return nil
		}
		b, err := os.ReadFile(path)
		if err != nil {
			return err
		}
		var s Snapshot
		if err := json.Unmarshal(b, &s); err != nil {
			return err
		}
		snaps = append(snaps, s)
		return nil
	})
	return snaps, err
}
