package config

import (
	"strings"
	"time"
)

func (c RuntimeConfig) MiningModeNormalized() string {
	mode := strings.ToLower(strings.TrimSpace(c.MiningMode))
	if mode == "" {
		return "always"
	}
	return mode
}

func (c RuntimeConfig) InScheduleWindow(now time.Time) bool {
	startMin, okStart := parseClockMinutes(c.ScheduleStart)
	endMin, okEnd := parseClockMinutes(c.ScheduleEnd)
	if !okStart || !okEnd {
		return true
	}

	nowMin := now.Hour()*60 + now.Minute()
	if startMin == endMin {
		return true
	}
	if startMin < endMin {
		return nowMin >= startMin && nowMin < endMin
	}
	return nowMin >= startMin || nowMin < endMin
}

func parseClockMinutes(value string) (int, bool) {
	value = strings.TrimSpace(value)
	if value == "" {
		return 0, false
	}
	parsed, err := time.Parse("15:04", value)
	if err != nil {
		parsed, err = time.Parse("15:04:05", value)
		if err != nil {
			return 0, false
		}
	}
	return parsed.Hour()*60 + parsed.Minute(), true
}
