package scheduler

import (
	"errors"
	"fmt"
	"strings"
)

// CronConfig represents the 5-field cron format (min hour dom mon dow).
// This engine only generates expressions; it does not install jobs.
type CronConfig struct {
	Minute string
	Hour   string
	Dom    string
	Month  string
	Dow    string
}

func (c CronConfig) Expression() (string, error) {
	fields := []string{
		strings.TrimSpace(c.Minute),
		strings.TrimSpace(c.Hour),
		strings.TrimSpace(c.Dom),
		strings.TrimSpace(c.Month),
		strings.TrimSpace(c.Dow),
	}
	for i, f := range fields {
		if f == "" {
			return "", errors.New("cron: all fields must be non-empty")
		}
		// Lightweight sanity: disallow whitespace inside a field.
		if strings.ContainsAny(f, " \t") {
			return "", fmt.Errorf("cron: field %d contains spaces", i)
		}
	}
	return strings.Join(fields, " "), nil
}

// ExampleLine generates a commented cron line showing how the expression might be used.
// command should be the CLI invocation (e.g. "/usr/local/bin/sysmon --snapshot --export json").
func ExampleLine(expr, command string) string {
	cmd := strings.TrimSpace(command)
	if cmd == "" {
		cmd = "sysmon --snapshot"
	}
	return fmt.Sprintf("# %s %s", expr, cmd)
}

