package scheduler

import (
	"errors"
	"time"
)

// Mode defines scheduler behavior.
type Mode string

const (
	ModeInterval Mode = "interval"
	ModeRolling  Mode = "rolling"
	ModeCronGen  Mode = "cron"
)

// IntervalConfig triggers a job on a fixed cadence.
type IntervalConfig struct {
	Every      time.Duration // required
	StopAfter  time.Duration // optional; 0 means run until stopped
	RunOnStart bool          // optional
}

// RollingConfig is a placeholder for the rolling-average mode.
// (We will wire this in once interval mode is stable.)
type RollingConfig struct {
	SampleEvery  time.Duration // cadence for sampling (e.g. 1s, 5s)
	Window       time.Duration // rolling window size (e.g. 60s)
	EmitEvery    time.Duration // how often to emit/export an aggregate snapshot
	StopAfter    time.Duration // optional
	RunOnStart   bool          // optional
	TrackPeaks   bool          // optional
	TrackMinMax  bool          // optional
	TrackAverage bool          // optional
}

// Config is the full scheduler configuration.
type Config struct {
	Mode     Mode
	Interval IntervalConfig
	Rolling  RollingConfig
}

func (c Config) Validate() error {
	switch c.Mode {
	case ModeInterval:
		if c.Interval.Every <= 0 {
			return errors.New("interval: Every must be > 0")
		}
		if c.Interval.StopAfter < 0 {
			return errors.New("interval: StopAfter cannot be negative")
		}
	case ModeRolling:
		// Allow placeholders for now but ensure it isn't obviously broken.
		if c.Rolling.SampleEvery <= 0 {
			return errors.New("rolling: SampleEvery must be > 0")
		}
		if c.Rolling.Window <= 0 {
			return errors.New("rolling: Window must be > 0")
		}
		if c.Rolling.EmitEvery <= 0 {
			return errors.New("rolling: EmitEvery must be > 0")
		}
		if c.Rolling.StopAfter < 0 {
			return errors.New("rolling: StopAfter cannot be negative")
		}
	case ModeCronGen:
		// Cron generation doesn't run a loop.
		return nil
	default:
		return errors.New("unknown scheduler mode")
	}
	return nil
}

