package config

// ResourceConfig centralizes pacing and resource optimization knobs.
type ResourceConfig struct {
	PodmanCPUs           string `yaml:"podman_cpus" toml:"podman_cpus"`
	PodmanMemory         string `yaml:"podman_memory" toml:"podman_memory"`
	MaxWorkers           int    `yaml:"max_workers" toml:"max_workers"`
	DesiredRPSPerWorker  int    `yaml:"desired_rps_per_worker" toml:"desired_rps_per_worker"`
	RequestsPerSec       int    `yaml:"requests_per_sec" toml:"requests_per_sec"`
	RequestBurstOverride int    `yaml:"request_burst" toml:"request_burst"`
}

// ApplyDefaults ensures sane values without requiring every field to be set.
func (r *ResourceConfig) ApplyDefaults() {
	if r.MaxWorkers < 1 {
		r.MaxWorkers = 1
	}
	if r.DesiredRPSPerWorker < 1 {
		r.DesiredRPSPerWorker = DefaultDesiredRPSPerWorker
	}
	if r.PodmanCPUs == "" {
		r.PodmanCPUs = DefaultPodmanCPUs
	}
	if r.PodmanMemory == "" {
		r.PodmanMemory = DefaultPodmanMemory
	}
}

// EffectiveRequestsPerSec returns an auto-derived value when not explicitly set.
func (r ResourceConfig) EffectiveRequestsPerSec() int {
	if r.RequestsPerSec > 0 {
		return r.RequestsPerSec
	}

	rps := r.MaxWorkers * r.DesiredRPSPerWorker
	if rps < 1 {
		return 1
	}
	return rps
}
