package vsphere

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

	UserData string
}

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

	// 1. Set Hardcoded Fallbacks (Safety Net)
	d := Defaults{
		CP_CPU: 2, CP_Mem: 4, CP_Disk: 40, // vSphere might need different defaults than Harvester
	}

	// 2. Read from _defaults block
	if defs, ok := raw["_defaults"].(map[string]interface{}); ok {

		// Profile Defaults
		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
			}
		}

		// Helm Chart Defaults
		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
			}
		}

		// UserData Default
		if v, ok := defs["userData"].(string); ok {
			d.UserData = v
		}
	}
	return d, nil
}

// GetBaseValues returns the raw bytes for the MasterBuilder
func GetBaseValues() []byte {
	return valuesYAML
}
