package harvester

import (
	_ "embed"
	"fmt"

	"gopkg.in/yaml.v3"
)

//go:embed values.yaml
var valuesYAML []byte

type Defaults struct {
	CP_CPU  int
	CP_Mem  int
	CP_Disk int

	ChartRepo    string
	ChartName    string
	ChartVersion string

	// [NEW] Default UserData for this provider
	UserData string
}

func GetDefaults() (Defaults, error) {
	var raw map[string]interface{}
	if err := yaml.Unmarshal(valuesYAML, &raw); err != nil {
		return Defaults{}, fmt.Errorf("failed to parse harvester base values: %w", err)
	}

	d := Defaults{
		CP_CPU: 4, CP_Mem: 8, CP_Disk: 40, // Safety Fallbacks
	}

	if defs, ok := raw["_defaults"].(map[string]interface{}); ok {
		if cp, ok := defs["controlPlaneProfile"].(map[string]interface{}); ok {
			if v, ok := cp["cpuCores"].(int); ok {
				d.CP_CPU = v
			}
			if v, ok := cp["memoryGb"].(int); ok {
				d.CP_Mem = v
			}
			if v, ok := cp["diskGb"].(int); ok {
				d.CP_Disk = v
			}
		}
		if chart, ok := defs["helmChart"].(map[string]interface{}); ok {
			if v, ok := chart["repo"].(string); ok {
				d.ChartRepo = v
			}
			if v, ok := chart["name"].(string); ok {
				d.ChartName = v
			}
			if v, ok := chart["version"].(string); ok {
				d.ChartVersion = v
			}
		}
		// [NEW] Extract UserData
		if v, ok := defs["userData"].(string); ok {
			d.UserData = v
		}
	}
	return d, nil
}

func GetBaseValues() []byte {
	return valuesYAML
}
